Unit-Ii Programming Constructs
Unit-Ii Programming Constructs
PROGRAMMING CONSTRUCTS
TOPICS
Variables - primitive data types-
identifiers naming conventions -
keywords literals -operators – binary -
unary and ternary expression -
precedence rules and associativity -
primitive type conversion and casting -
flow of control - arrays- command line
arguments
Variables
• A variable is a container which holds the value
while the program is executed. A variable is
assigned with a data type.
• Variable is a name of memory location.
• Three types of variables in java,
1.local
2.instance
3.static
• int num=12345;
class A
{
int data=50; //instance variable
static int m=100; //static variable
void method()
{
int n=90; //local variable
}
}//end of class
1) Local Variable - A variable declared inside the body
of the method is called local variable. We can use
this variable only within that method. A local
variable cannot be defined with "static" keyword
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
Class
• It should start with the uppercase letter.
• It should be a noun such as Color, Button,
System, Thread, etc.
• Use appropriate words, instead of acronyms.
public class Employee
Naming conventions
Interface
• It should start with the uppercase letter.
• It should be an adjective such as Runnable,
Remote, ActionListener.
• Use appropriate words, instead of acronyms.
interface Printable
Naming conventions
Method
• It should start with lowercase letter.
• It should be a verb such as main(), print(), println().
• If the name contains multiple words, start it with a
lowercase letter followed by an uppercase letter
such as actionPerformed()
void draw()
{
}
Naming conventions
Variable
• It should start with a lowercase letter such as
id, name
• It should not start with the special characters
like & (ampersand), $ (dollar), _ (underscore)
• If the name contains multiple words, start it
with the lowercase letter followed by an
uppercase letter such as firstName, lastName.
• Avoid using one-character variables such as x,
y, z Ex:id
Naming conventions
Package
• It should be a lowercase letter such as java,
lang
prefix ++expr --expr +expr -expr ~
!
additive +-
equality == !=
bitwise exclusive OR ^
bitwise inclusive OR |
logical OR ||
Ternary ternary ?:
class OperatorExample
{
public static void main(String args[])
{
int x=10;
System.out.println(x++);
System.out.println(++x);
System.out.println(x--);
System.out.println(--x);
}
}
class Incdec
{
public static void main(String args[])
{
int a=10;
int b=10;
System.out.println(a++ + ++a);//10+12=22
System.out.println(b++ + b++);//10+11=21
}
}
class Arith
{
public static void main(String args[])
{
int a=10;
int b=5;
System.out.println(a+b); //15
System.out.println(a-b); //5
System.out.println(a*b); //50
System.out.println(a/b); //2
System.out.println(a%b); //0
}
}
class OperatorExample
{
public static void main(String args[])
{
int a=2;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}
}
Operator Precedence
• Operator precedence determines the order in
which the operators in an expression are
evaluated.
• BODMAS
Operator Type Precedence
Unary expr++ expr--
++expr --expr +expr -expr ~ !
Arithmetic */%
+-
== !=
Bitwise &
Logical &&
||
Ternary ?:
= += -= *= /= %=
assignment &= ^= |= <<= >>= >>>= right to left
Type conversion
• Type casting is a method or process that
converts a data type into another data type in
both ways manually and automatically.
if(condition1)
{
}
else if(condition2)
{
}
else if(condition3)
{
}
else{
}
public class IfElseIfExample {
public static void main(String[] args) {
int marks=65;
if(marks<50){
System.out.println("fail");
}
else if(marks>=50 && marks<60){
System.out.println("D grade");
}
else if(marks>=60 && marks<70){
System.out.println("C grade");
}
else if(marks>=70 && marks<80){
System.out.println("B grade");
}
else if(marks>=80 && marks<90){
System.out.println("A grade");
}else if(marks>=90 && marks<100){
System.out.println("A+ grade");
}else{
System.out.println("Invalid!");
} } }
Switch Statement
• for loop
• while loop
• do- while loop
for loop
int[][] arr=new int[3][3];
class Testarray
{
public static void main(String args[])
{
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
Programs
1.Fibonacci Series
2.Prime Number Program
3.Palindrome Program
4.Factorial Program
5.Armstrong Number
6.Print the duplicate elements of an array
7.Print the largest element in an array
8.Print the smallest element in an array
9.Print the elements of an array present on odd position
10.Find 2nd Largest Number in an Array
11.Print Odd and Even Numbers of an array
import java.util.Scanner;
class Age
{
public static void main(String[] args)
{
Scanner myObj = new Scanner(System.in);
System.out.println("Enter your age :");
int age = myObj.nextInt();
System.out.println("Age is: " + age);
}
}
• compile by > javac Age.java
• run by > java Age
Command line arguments
• It is an argument, that is passed at the time of
running the java program
• run by > java Example Input
class Cse
{
public static void main(String args[])
{
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
}
}
• compile by > javac Cse.java
• run by > java Cse welcome 10 a
class Sum
{
public static void main(String ar[])
{
int x,y,s;
x=Integer.parseInt(ar[0]);
y=Integer.parseInt(ar[1]);
s=x+y;
System.out.println("sum of " + x + " and " + y +" is "
+s);
}
}
• compile by > javac Sum.java
• run by > java Sum 10 20