JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java...
-
Upload
brendan-johns -
Category
Documents
-
view
274 -
download
0
Transcript of JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java...
JAVA—Bitwise
Compiling/Executing a Java Application
Source Code
Java Compiler
ByteCode
<< .java file>>
<< javac .java >>
<< .class file>>
Java Interpreter
Machine Code
<< java [name of class] >>
Question 1
• Write a program in Java to print the sum of numbers passed as command line arguments.
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
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
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
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
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
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
Question 2
Write a program in java which prints all prime numbers from a list of numbers passed as command line arguments
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
• >> [Right Shift]
• << [Left Shift]
• >>> [Unsigned Right Shift]
Q3Use of >> , << , >>> operators
>> 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
<< 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
>>> 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
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
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
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
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
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);}}
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);}}
Q4(b)Predict The output
for(byte b=0; b< 300 ; b++)System.out.println(b);
(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
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);}}
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”);}}
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
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
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
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
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
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