JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java...

31
JAVA—Bitwise

Transcript of JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java...

Page 1: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

JAVA—Bitwise

Page 2: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

Compiling/Executing a Java Application

Source Code

Java Compiler

ByteCode

<< .java file>>

<< javac .java >>

<< .class file>>

Java Interpreter

Machine Code

<< java [name of class] >>

Page 3: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

Question 1

• Write a program in Java to print the sum of numbers passed as command line arguments.

Page 4: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

class commandSum{public static void main(String x[ ]){

double sum =0;for(int i=0; i<x.length;i++)sum = sum + Double.parseDouble(x[i]);System.out.println("Sum is"+sum);

} // End of main} // End of CommandSum

Page 5: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

System.out.println()

• Prints/Displays output and shifts the print control to new line (Similar printf(“\n”) in C)

• Displays output only in String form• If parameter to it is not in String form

then it will be converted to string form by internally calling toString()

• + operator can be used to concatenate data from different types

Page 6: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

Examples

• System.out.println(“Hello”+10);• System.out.println(10+20);• System.out.println(“10”+20);• System.out.println(“Hello: ”+20+”is my age”);

Note :

+ opeartor is used for dual purpose addition,concatenation

Hello10

301020

Hello20is my age

Page 7: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

System.out.print()

• Prints/Displays output starting from the same line (Similar printf() in C)

• Displays output only in String form

• If parameter to it is not in String form then it will be converted to string form by internally calling toString()

• + operator can be used to concatenate data from different types

Page 8: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

Examplesclass test104{public static void main(String args[]){System.out.print("Hello");System.out.print("I am fine");System.out.println(" It is OK");}}

D:\java\bin>java test104HelloI am fine It is OK

Page 9: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

Example 2class test105{public static void main(String args[]){System.out.print("Hello");System.out.print("I am fine");System.out.println(" It is OK");System.out.println(" It is OK Again");}}

D:\java\bin>java test105HelloI am fine It is OK It is OK Again

Page 10: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

Question 2

Write a program in java which prints all prime numbers from a list of numbers passed as command line arguments

Page 11: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

class commandPrime{public static void main(String x[ ]){

for(int i=0; i<x.length;i++){int number = Integer.parseInt(x[i]);boolean flag = true;for (int j=2; j< number/2 ;j++){

if( number % j ==0) {

flag = false;break;

}} // End of inner forif(flag) System.out.println(" "+number);} // End of outer for

} // End of main} // End of CommandPrime

Page 12: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

• >> [Right Shift]

• << [Left Shift]

• >>> [Unsigned Right Shift]

Q3Use of >> , << , >>> operators

Page 13: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

>> Signed Right Shift>> Signed Right Shift operator shifts the operand right by

preserving the sign of the number

Usage :

x >> k ;

If x is byte,short or int type then bits of x is shifted k % 32 times. If x is long then bits of x is shifted k % 64 times.

Note : Shifting x right each bit will divide the x by 2

S b0b2b2bn-1 ………012n-1

S b1b2b3S ……… Discarded

Page 14: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

<< Left Shift<< Left Shift operator shifts the operand left (bit positions)

Shifting is done into the sign bit. Earlier sign bit is lost

Usage :

x << k ; [ Each Left shift multiplies x by 2]

If x is byte,short or int type then bits of x is shifted k % 32 times. If x is long then bits of x is shifted k % 64 times.

S b0b1b2bn-1 ………012n-1

bn-1 0b0b1bn-21 ………Discarded

Page 15: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

>>> Unsigned Right Shift

• For Positive Numbers Both >> and >>> work same

• Unsigned right shift operator shifts the operand right by inserting 0 in the sign bit.

S b0b1b2bn-1 ………012n-1

0 b1b2b3SDiscarded

Page 16: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

class rttest{public static void main (String args[ ]){byte b = -10;System.out.println(b>>8);System.out.println(b>>9);System.out.println(b>>16);System.out.println(b>>17);System.out.println(b>>31);System.out.println(b>>32);System.out.println(b>>33);

short s = -10;System.out.println(s>>8);System.out.println(s>>9);System.out.println(s>>16);System.out.println(s>>17);System.out.println(s>>32);System.out.println(s>>32);System.out.println(s>>33);

-1-1-1-1-1-10-5

-1

-1-1-10-10-5

-1

Page 17: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

b = 10;s = 10;System.out.println(b>>8);System.out.println(s>>16);System.out.println(b>>9);System.out.println(s>>17);

long l = -10;System.out.println(l>>32);System.out.println(l>>33);System.out.println(l>>15);System.out.println(l>>16);System.out.println(l>>63);System.out.println(l>>64);System.out.println(l>>65);

0000

-1-1-1-1-1-10-5

Page 18: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

class lttest{public static void main(String args[ ]){byte b = -10;System.out.println(b<<8);System.out.println(b<<9);System.out.println(b<<16);System.out.println(b<<17);System.out.println(b<<31);System.out.println(b<<32);System.out.println(b<<33);

short s = -10;System.out.println(s<<8);System.out.println(s<<9);System.out.println(s<<16);System.out.println(s<<17);System.out.println(s<<32);System.out.println(s<<32);System.out.println(s<<33);

b = 10;s = 10;System.out.println(b<<8);System.out.println(s<<16);System.out.println(b<<9);System.out.println(s<<17);

long l = -10;System.out.println(l<<32);System.out.println(l<<33);System.out.println(l<<15);System.out.println(l<<16);System.out.println(l<<63);System.out.println(l<<64);System.out.println(l<<65);

}}

-2560-5120-655360-13107200-10-20

-2560-5120-655360-1310720-10-10-20

256065536051201310720

-42949672960-85899345920-327680-6553600-10-20

Page 19: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

class leftRight{public static void main(String args[]){int x =40;System.out.println(x>>2); System.out.println(x<<2);System.out.println(x>>>2);

int x1 =-40;System.out.println(x1>>2); System.out.println(x1<<2);System.out.println(x1>>>2);

int x2 =-256;System.out.println(x2>>32); System.out.println(x2<<32);System.out.println(x2>>>32);

int x3 =256;System.out.println(x3>>32); System.out.println(x3<<32);System.out.println(x3>>>32);

Predict The Output for this code

Page 20: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

int x4 = 32;System.out.println(x4>>7); System.out.println(x4<<7);System.out.println(x4>>>7);

int x5 = 1024;System.out.println(x5>>31); System.out.println(x5<<31);System.out.println(x5>>>31);

int x6 = 1024;System.out.println(x6>>33); System.out.println(x6<<33);System.out.println(x6>>>33);

int x7 = -1024;System.out.println(x7>>33); System.out.println(x7<<33);System.out.println(x7>>>33);}}

Page 21: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

Q4(a)Predict Outputs

class typetest{public static void main(String args[]){byte b = 127;System.out.println(++b);byte b1 = -128;System.out.println(--b1);short s = 32767;System.out.println(++s);short s1 = -32768;System.out.println(--s1);}}

Page 22: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

Q4(b)Predict The output

for(byte b=0; b< 300 ; b++)System.out.println(b);

Page 23: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

(i) byte b =0(ii) byte b = 200; (iii) boolean b = 1; (iv) int a = 10.56;(v) float f = 12.67; (vi) short s = 23; (vii) short s1 = 33000;

Q5(a) Find the invalid assignments from the following

Page 24: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

Q 5(b)Predict the output of the code

class typecasting{public static void main(String args[]){int a = 400;byte b = (byte) a;System.out.println(b);int a1 = (int) 10.56;System.out.println(a1);char x = (char) a;System.out.println(x);float f = (float)10.65;System.out.println(f);}}

Page 25: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

Q6 Consider the following program and predict output for following command line executions (i) java test (ii) java A (iii) java B

// test.javaclass A{public static void main(String args[ ]){System.out.println(“Hello This is class A”);}}class B{public static void main(String args[ ]){System.out.println(“Hello This is class B”);}}

Page 26: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

What’s output for the followingbyte b1 = 10;byte b2 = 5;byte b3 = b2 * b1;System.out.println("b3="+b3);

short s1 = 10;short s2 = 20;short s3;s3 = s1 * s2;System.out.println("s3="+s3);

float f = 10.00f;int a =10;if( a == f)System.out.println("Hello");elseSystem.out.println("Hi");

possible loss of precisionfound : intrequired: bytebyte b3 = b2 * b1;

possible loss of precisionfound : intrequired: shortshort s3 = s1 * s2;

Hello

Page 27: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

float a1 = 4.57f;double b1 = 4.57;if( a1 == b1)System.out.println("Hello");elseSystem.out.println("Hi");

float a2 = 4.5f;double b2 = 4.5;if( a2 == b2)System.out.println("Hello");elseSystem.out.println("Hi");

Hi

Hello

Page 28: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

Instance Fields vs Local Variables

• Local Variable is any variable which is declared inside the method

• Local variable has to be initialized before use

• Instance Fields belongs to objects and are allocated space when object is created

• Instance fields are by default initialized to some initial values depending upon their type

Page 29: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

Example (Local Variable)

class lttest{public static void main(String args[ ]){int x;int y = x + 10;System.out.println("y="+y);}}

E:\Java Programs> javac lttest.javalttest.java:6: variable x might not have been initializedint y = x + 10; ^1 error

Page 30: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

Example (Instance Fields)class X{ }class Y{ }class Z{ }class XYZ{private int x;private float y;private double z;private char a;private boolean b;private byte by;private short sh;private long lo;private X x1;private Y y1;private Z z1;

void show(){System.out.println("x="+x);System.out.println("y="+y);System.out.println("z="+z);System.out.println("a="+a);System.out.println("b="+b);System.out.println("by="+by);System.out.println("sh="+sh);System.out.println("lo="+lo);System.out.println("x1="+x1);System.out.println("y1="+y1);System.out.println("z1="+z1);}// End of show()}// End of class XYZ

// test.java

Page 31: JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

class test{public static void main(String[ ] x){XYZ x1 = new XYZ();x1.show();}}

E:\Java Programs>java testx=0y=0.0z=0.0a=b=falseby=0sh=0lo=0x1=nully1=nullz1=null