Download - java Basic Programming Needs
![Page 1: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/1.jpg)
Java Session 4
![Page 2: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/2.jpg)
Contents…
Data TypesType Conversion and CastingVariablesScope and lifetime of variablesOperatorsExpressionsControl Statements
![Page 3: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/3.jpg)
DATA TYPES
• Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
• Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory
![Page 4: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/4.jpg)
DATA TYPES
![Page 5: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/5.jpg)
Data Types• There are two data types available in Java:
Primitive Data TypesReference/Object Data Types
byte:Byte data type is a 8-bit signed two.s complement integer.Minimum value is -128 (-2^7)Maximum value is 127 (inclusive)(2^7 -1)Default value is 0Byte data type is used to save space in large arrays, mainly in place
of integers, since a byte is four times smaller than an int.Example : byte a = 100 , byte b = -50
![Page 6: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/6.jpg)
short:Short data type is a 16-bit signed two's
complement integer.Minimum value is -32,768 (-2^15)Maximum value is 32,767(inclusive) (2^15 -1)Default value is 0.Example : short s= 10000 , short r = -20000
![Page 7: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/7.jpg)
int:Int data type is a 32-bit signed two's complement integer.Minimum value is - 2,147,483,648.(-2^31)Maximum value is 2,147,483,647(inclusive).(2^31 -1)Int is generally used as the default data type for integral values
unless there is a concern about memory.The default value is 0.Example : int a = 100000, int b = -200000
long:Long data type is a 64-bit signed two's complement integer.Minimum value is -9,223,372,036,854,775,808.(-2^63)Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1)This type is used when a wider range than int is needed.Default value is 0L.Example : int a = 100000L, int b = -200000L
![Page 8: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/8.jpg)
float:Float data type is a single-precision 32-bit IEEE 754 floating point.Float is mainly used to save memory in large arrays of floating point
numbers.Default value is 0.0f.Float data type is never used for precise values such as currency.Example : float f1 = 234.5f
double:double data type is a double-precision 64-bit IEEE 754 floating point.This data type is generally used as the default data type for decimal
values. generally the default choice.Double data type should never be used for precise values such as
currency.Default value is 0.0d.Example : double d1 = 123.4
![Page 9: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/9.jpg)
boolean:boolean data type represents one bit of information.There are only two possible values : true and false.This data type is used for simple flags that track true/false
conditions.Default value is false.Example : boolean one = true
char:char data type is a single 16-bit Unicode character.Minimum value is '\u0000' (or 0).Maximum value is '\uffff ' (or 65,535 inclusive).Char data type is used to store any character.Example . char letterA ='A'
![Page 10: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/10.jpg)
Reference Datatypes• Reference variables are created using defined constructors
of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. For example, Employee, Puppy etc.
• Class objects, and various type of array variables come under reference data type.
• Default value of any reference variable is null.
• A reference variable can be used to refer to any object of the declared type or any compatible type.
• Example : Animal animal = new Animal("giraffe");
![Page 11: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/11.jpg)
Data TypesDataType Memory (Bytes) Range
byte 1 -128 to 127
short 2 -32768 t0 32767
int 4 -2147483648 to 2147483647
long 8 -922337263685477808 to -922337263685477807
float 4 1.4e-045 to 3.4e+038
double 8 4.9e-324 to 1.8e+308
char 2 0 to 65536
boolean - True/False
![Page 12: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/12.jpg)
TYPE CONVERSIONTYPE CONVERSION Size Direction of Data Type
Widening Type Conversion (Casting down) Smaller Data Type Larger Data Type
Narrowing Type Conversion (Casting up) Larger Data Type Smaller Data Type
Conversion done in two ways Implicit type conversion
Carried out by compiler automatically Explicit type conversion
Carried out by programmer using casting
![Page 13: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/13.jpg)
Type ConversionType Conversion
Widening Type Converstion Implicit conversion by compiler automatically
byte -> short, int, long, float, doubleshort -> int, long, float, doublechar -> int, long, float, double
int -> long, float, doublelong -> float, double
float -> double
byte -> short, int, long, float, doubleshort -> int, long, float, doublechar -> int, long, float, double
int -> long, float, doublelong -> float, double
float -> double
![Page 14: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/14.jpg)
Type ConversionType Conversion
Narrowing Type ConversionProgrammer should describe the conversion
explicitly
byte -> charshort -> byte, charchar -> byte, short
int -> byte, short, charlong -> byte, short, char, int
float -> byte, short, char, int, longdouble -> byte, short, char, int, long, float
byte -> charshort -> byte, charchar -> byte, short
int -> byte, short, charlong -> byte, short, char, int
float -> byte, short, char, int, longdouble -> byte, short, char, int, long, float
![Page 15: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/15.jpg)
Type Casting
• General form: (targetType) value Examples: 1) integer value will be reduced module bytes
range:int i;byte b = (byte) i;
2) floating-point value will be truncated to integer value: float f; int i = (int) f;
![Page 16: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/16.jpg)
VARIABLES• type identifier [ = value][, identifier [= value] ...] ; • Here are several examples of variable declarations of various
types int a, b, c; // declares three ints, a, b, and c. int d = 3, e, f = 5; // declares three more ints, initializing
// d and f. byte z = 22; // initializes z. double pi = 3.14159; // declares an approximation of pi. char x = 'x'; // the variable x has the value 'x'.
![Page 17: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/17.jpg)
Types of variables
There are three kinds of variables in Java:Local variablesInstance variablesClass/static variables
Local variables are declared in methods, constructors, or blocksInstance variables are declared in a class, but outside a method,
constructor or any block.Class variables also known as static variables are declared with
the static keyword in a class, but outside a method, constructor or a block.
![Page 18: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/18.jpg)
Variable Scope
![Page 19: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/19.jpg)
Scope Definition
A scope is defined by a block:
{…
}
A variable declared inside the scope is not visible outside:
{int n;
}n = 1;
![Page 20: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/20.jpg)
Example: Variable Scopeclass Scope {
public static void main(String args[]) {int x;x = 10;if (x == 10) {
int y = 20;System.out.println("x and y: " + x + “ " + y);
x = y * 2;}System.out.println("x is " + x + “y is” + y);
}}
![Page 21: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/21.jpg)
Variable Lifetime
Variables are created when their scope is entered by control flow and
destroyed when their scope is left:
1) A variable declared in a method will not hold its value between different
invocations of this method.
2) A variable declared in a block looses its value when the block is left.
3) Initialized in a block, a variable will be re-initialized with every re-entry.
Variables lifetime is confined to its scope!
![Page 22: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/22.jpg)
Example: Variable Lifetimeclass LifeTime {
public static void main(String args[]) {int x;for (x = 0; x < 3; x++) {
int y = -1;System.out.println("y is: " + y);y = 100;System.out.println("y is now: " + y);
}}
}
![Page 23: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/23.jpg)
OPERATOR TYPES
• Java operators are used to build value expressions.• Java provides a rich set of operators:
1) arithmetic (+,-,*,/,%)2) assignment (+=,-=,*=,/=,%=,=)3) relational (==,!=,<,>,<=,>=)4) logical (&&,||,!)5) bitwise (&,|,~,^,<<,>>,>>)
![Page 24: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/24.jpg)
Bit wise operators
~ ~op Inverts all bits
& op1 & op2 Produces 1 bit if both operands are 1
| op1 |op2 Produces 1 bit if either operand is 1
^ op1 ^ op2 Produces 1 bit if exactly one operand is 1
>> op1 >> op2 Shifts all bits in op1 right by the value of op2
<< op1 << op2 Shifts all bits in op1 left by the value of op2
![Page 25: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/25.jpg)
Other Operators
?:
[]
.
(params)
(type)
new
shortcut if-else statement
used to declare arrays, create arrays, access array elements
used to form qualified names
delimits a comma-separated list of parameters
casts a value to the specified type
creates a new object or a new array
instanceof determines if its first operand is an instance of the second
![Page 26: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/26.jpg)
Table: Operator Precedence
highest()++*+>>>==&^|&&||?:=lowest
[]--/->>>>=!=
.~%
<<<
!
<=
op=
![Page 27: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/27.jpg)
• An expression is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value.
• Examples of expressions are in bold below: int number = 0; anArray[0] = 100; System.out.println ("Element 1 at index 0: " + anArray[0]);
int result = 1 + 2; // result is now 3 if(value1 == value2) System.out.println("value1 == value2");
Expressions
![Page 28: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/28.jpg)
CONTROL STATEMENTS
• Java control statements cause the flow of execution to advance and branch based on the changes to the state of the program.
• Control statements are divided into three groups:
1) selection statements allow the program to choose different parts of the execution based on the outcome of an expression
2) iteration statements enable program execution to repeat one or more statements
3) jump statements enable your program to execute in a non-linear fashion
![Page 29: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/29.jpg)
Selection Statements
• Java selection statements allow to control the flow of program’s execution based upon conditions known only during run-time.
• Java provides four selection statements:1) if2) if-else3) if-else-if4) switch
![Page 30: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/30.jpg)
Iteration Statements
• Java iteration statements enable repeated execution of part of a program until a certain termination condition becomes true.
• Java provides three iteration statements:1) while2) do-while3) for4) for each(JDK1.5 version)
![Page 31: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/31.jpg)
• if-else
Syntax Example
if (<condition-1>) { // logic for true condition-1 goes here} else if (<condition-2>) { // logic for true condition-2 goes here} else { // if no condition is met, control comes here}
int a = 10;if (a < 10 ) { System.out.println(“Less than 10”);} else if (a > 10) { System.out.pritln(“Greater than 10”);} else { System.out.println(“Equal to 10”);}
Result: Equal to 10s
![Page 32: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/32.jpg)
Syntax Example
switch (<value>) { case <a>: // stmt-1 break; case <b>: //stmt-2 break; default:
//stmt-3
int a = 10;switch (a) { case 1: System.out.println(“1”); break; case 10: System.out.println(“10”); break; default: System.out.println(“None”);
Result: 10
• switch
![Page 33: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/33.jpg)
• do-while
Syntax Example
do { // stmt-1} while (<condition>);
int i = 0;do {System.out.println(“In do”); i++;} while ( i < 10);
Result: Prints “In do” 11 times
• whileSyntax Example
while (<condition>) {//stmt
}
int i = 0;while ( i < 10 ) { System.out.println(“In while”); i++;}
Result: “In while” 10 times
![Page 34: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/34.jpg)
• for
Syntax Example
for ( initialize; condition; expression){ // stmt}
for (int i = 0; i < 10; i++){ System.out.println(“In for”);}
Result: Prints “In do” 10 times
![Page 35: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/35.jpg)
Jump Statements
• Java jump statements enable transfer of control to other parts of program.
• Java provides three jump statements:1) break (3 uses:loop,switch,nested blocks)
2) continue3) return
• In addition, Java supports exception handling that can also alter the control flow of a program.
![Page 36: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/36.jpg)
TEST
I m not giving the answers for these basic questions,
please try and execute if possible. If you still want the answers then
copy and paste the entire question in google.
![Page 37: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/37.jpg)
What all gets printed when the following program is compiled and run. Select the two correct answers.
public class test { public static void main(String args[]) { int i, j=1; i = (j>1)?2:1; switch(i) { case 0: System.out.println(0); break; case 1: System.out.println(1); case 2: System.out.println(2); break; case 3: System.out.println(3); break; } } } a) 0 b) 1 c) 2 d) 3
![Page 38: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/38.jpg)
Which declaration of the main method below would allow a class to be started as a standalone program. Select the one correct answer.
a) public static void main(String args[])b) public static void MAIN(String args[])c) public static void main(String args)d) public static void main(char args[])e) static public void main(String args[])f) public static int main(char args[])
![Page 39: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/39.jpg)
Which of the following will compile without error
1) import java.awt.*;package Mypackage;class Myclass {}
2) package MyPackage;import java.awt.*;class MyClass{}
3) /*This is a comment */
package MyPackage;import java.awt.*;class MyClass{}
![Page 40: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/40.jpg)
What’s the difference between constructors and normal methods?
Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times and it can return a value or can be void.
![Page 41: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/41.jpg)
• Give a simplest way to find out the time a method takes for execution without using any profiling tool?
long start = System.currentTimeMillis ();method ();long end = System.currentTimeMillis ();
System.out.println (“Time taken for execution is ” + (end – start));
![Page 42: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/42.jpg)
public class test { public static void main(String args[]) { String s1 = "abc"; String s2 = "abc"; if(s1 == s2) System.out.println(1); else System.out.println(2); if(s1.equals(s2)) System.out.println(3); else System.out.println(4); } } a)1 b)2 c)3 d)4
![Page 43: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/43.jpg)
• Which of the following are legal array declarations. Select the three correct answers.
a) int i[5][];b) int i[][]; c) int []i[];d) int i[5][5]; e) int[][] a;
![Page 44: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/44.jpg)
What is the range of values that can be specified for an int. Select the one correct answer. The range of values is compiler dependent.
a) -231 to 231 - 1b) -231-1 to 231
c) -215 to 215 - 1d) -215-1 to 215
![Page 45: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/45.jpg)
What would be the results of compiling and running the following class. Select the one correct answer.
class test { public static void main() { System.out.println("test"); } }
a)The program does not compile as there is no main method defined.
b)The program compiles and runs generating an output of "test" c)The program compiles and runs but does not generate any output. d)The program compiles but does not run.
![Page 46: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/46.jpg)
What gets printed when the following program is compiled and run. Select the one correct answer.
class test {
public static void main(String args[]) {int i,j,k,l=0;k = l++;j = ++k;i = j++;System.out.println(i);}
}
a) 0 b) 1 c) 2 d) 3
![Page 47: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/47.jpg)
• Which operator is used to perform bitwise inversion in Java. Select the one correct answer.
a) ~ b) ! c) & d)| e) ^
![Page 48: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/48.jpg)
What gets printed when the following program is compiled and run. Select the one correct answer.
public class test {
public static void main(String args[]) {byte x = 3;x = (byte)~x;System.out.println(x);
}}
a) 3 b) 0 c) 1 d) 11 e) 252 f) 214 g) 124 h) -4
![Page 49: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/49.jpg)
What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.
public class test {
public static void main(String args[]) {int x,y;x = 3 & 5;y = 3 | 5;System.out.println(x + " " + y);
}}
a) 7 1b) 3 7c) 1 7d) 3 1e) 1 3f) 7 3g) 7 5
![Page 50: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/50.jpg)
What all gets printed when the following program is compiled and run. Select the one correct answer.
public class test { public static void main(String args[]) { int i=0, j=2; do { i=++i; j--; } while(j>0); System.out.println(i); } }
a) 0 b) 1 c) 2 d) The program does not compile because of statement "i=++i;"
![Page 51: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/51.jpg)
What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.
public class test {
public static void main(String args[]) {int x, y;
x = 5 >> 2;y = x >>> 2;
System.out.println(y);}
}
a) 5b) 2c) 80d) 0e) 64
![Page 52: java Basic Programming Needs](https://reader034.vdocuments.us/reader034/viewer/2022042714/555c3da4d8b42a2c068b4b71/html5/thumbnails/52.jpg)
Thank You