Basic Syntactical Constructs in Java
Basic Syntactical Constructs in Java
CONSTRUCTS IN JAVA
BASIC SYNTACTICAL CONSTRUCTS IN JAVA
5) Define jdk. List the tools available in jdk? Explain any one?
(Summer 15) 4 marks
Definition: A Java Development Kit (JDK) is a collection of tools
which are used for developing, designing, debugging, executing
and running java programs.
Tools of JDK.
Java
Javap
Javah
Javadoc
jdb
appletviewer
javac
a) Java Compiler – it is used to translate java source code to
byte code files that the interpreter can understand
{
code=0;
price=0;
}
Item_details(int e,float p)
{
code=e;
price=p;
}
void putdata()
{
System.out.println("Code of item :"+code);
System.out.println("Price of item:"+price);
}
public static void main(String args[]) throws IOException
{
intco;floatpr;
BufferedReaderbr=new BufferedReader (new
InputStreamReader(System.in));
System.out.println("enter code and price of item");
co=Integer.parseInt(br.readLine());
pr=Float.parseFloat(br.readLine());
Item_detailsobj=new Item_details(co,pr);
obj.putdata();
}
}
{
this.accno = accno;
this.accname = accname;
this.balance = balance;
}
void deposite(double deposit_amount)
{
balance = balance+deposit_amount;
System.out.println("Your new available balance is"+balance);
}
void withdraw(double amount)
{
if(balance > amount)
{
balance = balance-amount;
System.out.println("Your current balance"+balance);
}
else if( balance == amount)
{
System.out.println("Your current balance is "+balance+". Your
minimum balance should be 1000. Hence cannot withdraw.");
}
else
{
System.out.println("Insufficient balance");
}
}
public static void main(String args[])
{
BufferedReader bin = new BufferedReader(new
InputStreamReader(System.in));
Account a;
double amount, bal;
try
{
System.out.println("Enter the account name account number and
balance");
String a_name = bin.readLine();
BASIC SYNTACTICAL CONSTRUCTS IN JAVA
Scope of Variable:
a) We can declare variables within any block
b) One block equal to one new scope in Java thus each time
you start a new block, you are creating a new scope.
c) A scope determines what objects are visible to other parts of
your program. It also determines the lifetime of those objects.
BASIC SYNTACTICAL CONSTRUCTS IN JAVA
Program:
{
static int a = 0; //class variable
int b = 0; // instance variable
VariableTypes()
{
a++;
b++;
int c = 0; //local variable
c++;
System.out.println("In constructor printing a: "+a);//will be accessed
System.out.println("In constructor printing b: "+b);//will be accessed
System.out.println("In constructor printing c: "+c);//will be accessed
}
public static void main(String ar[])
{
VariableTypes s = new VariableTypes();
VariableTypes s1 = new VariableTypes();
VariableTypes s2 = new VariableTypes();
System.out.println("in main printing a: "+VariableTypes.a);//will be
accessed
System.out.println("in main printing b: "+s.b);//will be accessed
System.out.println("in main printing c "+s.c);//will not be accessed
because this is a local variable declared in constructor
}
}
10) What is type casting? Explain and its types with proper
syntax and example. (Summer 18) 4 marks
Assigning a value of one type to a variable of another type is
known as Type Casting
There are 2 types of type casting
a) Widening or Implicit type casting
b) Narrowing or Explicit type casting
BASIC SYNTACTICAL CONSTRUCTS IN JAVA
11) Write all primitive data types available in java with their
storage sizes in bytes. (Summer 17) 4 marks
// return a power of 2
System.out.println("exp of a is: " +Math.exp(x));
boolean A=true;
boolean B=false;
System.out.println(“A\B”+(a|B)); //true
System.out.println(“A&B”+(A&B)); // false
System.out.println(“!A”+(!A)); // false
System.out.println(“A^B”+(A^B)); // true
System.out.println(“(A|B)&A”+((A|B)&A)); // true
}
}
<=- This operator returns true if the first expression is less than or
equal to the second expression else returns false.
if(Exp1< =exp2) {
do this
} else {
do this
BASIC SYNTACTICAL CONSTRUCTS IN JAVA
Example:
class RelationalOps {
public static void main(String args[]) {
int a = 10;
int b = 20;
System.out.println("a == b = " + (a == b) );
System.out.println("a != b = " + (a != b) );
System.out.println("a > b = " + (a > b) );
System.out.println("a < b = " + (a < b) );
System.out.println("b >= a = " + (b >= a) );
System.out.println("b <= a = " + (b <= a) );
}
BASIC SYNTACTICAL CONSTRUCTS IN JAVA
e) The Left Shift (<<): the left shift operator, <<, shifts all of the
bits in a value “to the left a specified number of times
specified by num”
General form: value <<num
e.g. x << 2 (x=12)
0000 1100 << 2 = 0011 0000 (decimal 48)
f) The Right Shift (>>): the right shift operator, >>, shifts all of
the bits in a value “to the right a specified number of times
specified by num”
General form: value >>num.
e.g. x>> 2 (x=32)
0010 0000 >> 2 = 0000 1000 (decimal 8)
specified by “num”
General form : value <<num
e.g. x << 2 (x=12)
0000 1100 << 2
= 0011 0000 (decimal 48)
b) The Right Shift (>>): the right shift operator, >>, shifts all of
the bits in a “value” to the right a specified number of times
specified by “num”
General form: value >>num
e.g. x>> 2 (x=32)
0010 0000 >> 2
= 0000 1000 (decimal 8)
class Sample
{
public static void main(String args[]){
inti,j,a=1;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.println(a+" ");
}
System.out.println(“\n”);
a++;
}
}
for(int i=1;i<=size;i++)
{
BASIC SYNTACTICAL CONSTRUCTS IN JAVA
for(int j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println("");
}
}
}
statement n;
Statement in block may be single statement or multiple
statement. If condition is true then statement block will
be executed otherwise block is skipped & statement n
is executed
if (test condition 1)
{
if (test condition2)
{
statement block 1;
}
else
{
statement block 2;
}
else
{
statement block 3;
}
statement n;
If condition-1 is false, statement block-3 will be
executed; otherwise it continues to perform second
test. If condition-2 true, the statement block-1 will be
evaluated; otherwise statement block-2 will be
evaluated & then control is transferred to statement n.
In nesting care should be taken for every if with else.
default statement;
statement x;
This construction is known as else……if ladder. The conditions are
evaluated from top (of the ladder) to downwards. As soon as true
conditions are found, statement associated with it is executed &
control is transferred to statement-n (skipping the rest of the
ladder). When all the n conditions becomes false, then final else
containing default statement will be executed.
Switch Statement:
a) Java has built-in multi way decision statement known as
switch. it can be used instead of if or nested if…..else.
b) General form:
switch(expression)
{
case value 1:
block 1;
break;
case value 2:
block 2;
break;
.
.
.
default:
default block;
break;
}
statement n;
c) The expression is an integer expression or character. value1,
value2…. Value n are constants or constant expressions
which must be evaluated to integral constants are known as
case labels. Each of these values should be unique within
switch statement. Block1, block 2 are statement lists & may
contain zero or more statements. There is no need to put
braces around these blocks but case labels must end with
colon ( : ).
BASIC SYNTACTICAL CONSTRUCTS IN JAVA
b) Continue:
The continue statement skips the current iteration of a for,
while , or do-while loop. The unlabeled form skips to the end
of the innermost loop's body and evaluates the boolean
expression that controls the loop.
A labeled continue statement skips the current iteration of an
outer loop marked with the given label.
Example:
public class Test {
public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
if( x == 30 ) {
continue;
}
BASIC SYNTACTICAL CONSTRUCTS IN JAVA
System.out.print( x );
System.out.print("\n");
}
}
}
Syntax:
for(data_type variable : array | collection)
{}
Example:
ClassForEach
{
public static void main(String args[])
{
intnums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0; // use for-each style for to display and sum the values
for(int x : nums)
{
System.out.println("Value is: " + x);
sum += x;
}
System.out.println("Summation: " + sum);
}
BASIC SYNTACTICAL CONSTRUCTS IN JAVA
Example:
public class SwitchExample {
BASIC SYNTACTICAL CONSTRUCTS IN JAVA
b) continue:
The continue statement skips the current iteration of a for,
while , or do-while loop. The unlabeled form skips to the end
of the innermost loop's body and evaluates the boolean
expression that controls the loop. A labeled continue
statement skips the current iteration of an outer loop marked
with the given label.
Example:
public class TestContinue
{
public static void main(String args[])
{
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers )
{
if( x == 30 )
{
continue;
}
System.out.print( x );
System.out.print("\n");
}
}
}
Class: A “class” is a user defined data type. Data and methods are
encapsulated in class. It is a template or a pattern which is used to
define its properties. Java is fully object oriented language. All
program code and data reside within objects and classes.
BASIC SYNTACTICAL CONSTRUCTS IN JAVA
Syntax :
Example:
class Student{
int id; //field or data member or instance variable
String name;
public static void main(String args[]){
Student s1=new Student(); //creating an object of Student
System.out.println(s1.id); //accessing member through reference
variable
System.out.println(s1.name);
}}