JAVA Notes
JAVA Notes
Example:
Java is Object oriented language, means Java supports class, object, encapsulation,
abstraction, inheritance, polymorphism, coupling, cohesion, association, aggregation
and composition.
Java supports non-oops concepts also like constants, variables, data types, control
statements, loops & arrays. Because of this, Java is not called as pure oop language.
Program:
package javaTests;
import java.util.Scanner;
Note 2: When we initialize variable with a value, we need to enclose that value with single
quote for ‘char’, need to enclose that the value with double quotes for “String”, and no need
to use any quotes for remaining types like byte, short, int, long, float, double and boolean.
It is a class in default focus, means it will be accessible in current package in current project.
Syntax – 2:
It is a class in public focus, means it will be accessible in any package of current project.
import java.util.Scanner;
% (modules) 10%2=0
++ increment by 1
-- decrement by 1
// comment
Example 1:
int x=10;
int y=++x; // pre increment (increase and then release)
here x=11 and then y=11.
Example 2:
int x=10;
int y=x++; // post increment (release and then increase)
here x=11 and then y=10.
Example 3:
int x=10;
int y=--x; //pre decrement (decrease and then release)
here x=9 and then y=9.
Example 4:
int x=10;
int y=x--; // post decrement (release and then decrease)
here x=9 and then y=10.
Example 5:
int x=10; int x=10;
++x; // individual statement x++;// individual statement
int y=x; int y=x;
both programs output is same
Example 6:
int x=10; int x=10;
--x; // individual statement x--;// individual statement
int y=x; int y=x;
both programs output is same
Example:
int x=10;
int y=10;
if (x==y) // compare values of variables
{
-----
-----
}
Example:
RemoteWebDriver driver1;
ChromeDriver driver2;
if (driver1==driver2) // compare addresses of memory locations of objects
{
-----
-----
}
Case Study:
x y
Kalam Kalam
1001 2001
if (condition)
{
x=a;
}
else
{
x=b;
}
Note:
Unary Binary Ternary
-, ++, --, ~, …etc. +, -, *, /, %, instance of, (condition)? a:b
Operator with one …etc.
operand Operator with two Operator with three
Ex: operands operands
-10, Ex:
x++, 10+20,
--y a+b,
e instanceof WebElement
x y x&y
0 0 0
0 1 0
1 0 0
1 1 1
binary - 0 and 1, 0 - false, 1 - true, when both are 1 only output is 1.
x y X^y
0 0 0
0 1 1
1 0 1
1 1 0
Both are same then result is 0, if different result is 1.
here, negation or complement (~) means:
x ~x
0 1
1 0
here, left shift operator (<<) means:
ex: 10<<3
step 1: binary code of 10:
0 0 0 0 1 0 1 0
0 1 0 1 0 0 0 0
Remove 3 digits left side and add three 0’s on right side.
step 4: convert to decimal number.
0 1 0 1 0 0 0 0
2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
0 + 64 + 0 + 16 + 0 + 0 + 0 + 0 = 80
here, Right shift operator (>>) means:
ex: 10>>3
step 1: get binary code for 10
1 0 1 0
step 2: make as 8 digits code by adding “0” s left side.
10 | P a g e JAVA NOTES
0 0 0 0 1 0 1 0
step 3: Remove 3 digits right side and no adding
0 0 0 0 1 0 1 0
0 0 0 0 1
step 4: convert to decimal number.
0 0 0 0 1
2^4 2^3 2^2 2^1 2^0
0+0+0+0+1=1
here, zero with Right shift operator (>>>) means:
ex: 10 >>> 3
step 1: get binary code for 10
1 0 1 0
0 0 0 0 1 0 1 0
0 0 0 0 0 0 0 1
step 4: convert to decimal number.
0 0 0 0 0 0 0 1
2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
0 + 0 + 0 + 0 + 0 + 0 + 0 +1 = 1
Note:
<< Left shift Remove digits at left side and add 0’s on
right side
>> Right shift remove digits at right side
>>> Zero with right shift Remove digits at right side and add 0’s
on left side
>> , >>> can give same output.
While running Java code, JRE can follow above (1 to 11) precedence while
running lengthy expressions.
12 | P a g e JAVA NOTES
If any brackets or method calls in expressions, those will run first before going
to above precedence.
Note 2: In Java language, “+” is overloaded operator. Here “+” works for addition
and concatenation.
Example:
10+2=12 // addition
“10” + “2” =102 // concatenation
Case Study:
int + int // addition
int + float // addition
float + int // addition
float + float // addition
addition for numeric values in byte, short, int, long, float, double.
char + char // addition
addition of ASCII values of chars
boolean + boolean // error
String + String //concatenation
boolean + any other type // error
any other type + boolean // error
numeric type + char // addition
char + numeric type // addition
addition of numeric type value with ASII value of char
String + numeric type //concatenation as String
numeric type + String //concatenation as String
String + char //concatenation as String
char + String //concatenation as String
13 | P a g e JAVA NOTES
Note 2:
When we used nextInt() and then nextLine() in code, we can get chance to enter
number only but won’t get chance to give next string value.
It’s because when we enter a number then press Enter, nextInt() consumes only the
number, not the “end of line”. When nextLine() executes, it consumes the “end of line”
still in the buffer from the first input.
2. Type conversion:
In type conversion, a data type is automatically converted into another data type
by a compiler at the compiler time (while writing code in Eclipse IDE).
In type conversion, the destination data type cannot be smaller than the source
data type, that’s why it is also called “widening conversion”.
One more important thing is that it can only be applied to compatible data types.
Type conversion examples:
Example:
int x=30;
float y;
14 | P a g e JAVA NOTES
y=x; //y==30.000000
Example:
int n1=31;
int n2=031; // octal
int n3=0x31; // hexadecimal
int n4=0b1001; // binary
System.out.println(n1); //31
System.out.println(n2); //25
System.out.println(n3); //49
System.out.println(n4); //9
Example:
long x=1000*60*60*24*365;
long y=1000*60*60*24*365L;
System.out.println(x);
// wrong output due to correct output size is more than 8 bytes.
System.out.println(y);
// correct output because of memory expansion due to “L” suffix.
Here L for long type and F for float type. If these are creating any confusion, we are able to
go to “BigInteger” and “BigDecimal” classes, which classes objects can allow us to work with
large values.
3.Parsing:
Telling the program to interpret (on runtime) a string into
integer/decimal/octal/binary/hexadecimal, etc.
Parsing is to read the value of one object and then convert it to another type.
For example:
String s= “10”; // “10” is string
int i=Integer.parseInt(s); // 10 is int
here, we may have a string with a value of “10”, Internally that string contains the characters
‘1’ and ‘0’. The method “parseInt()” takes that string value and returns a real number 10.
Example:
String x=driver.findElement(locator).getAttribute(“maxlength”);
int y=Integer.parseInt(x);
while formatting output text, the format specifiers for general strings, characters, and
numeric types are applied according to the following syntax:
16 | P a g e JAVA NOTES
% [argumentindex$] [flags] [width] [.pricision] Conversion
% |$ + 7 .2 f
l. loops:
To execute a block of code for multiple times, we can use loops/iterations.
Java can support 4 loops in 3 categories:
Condition Based Count Based List Based
while For for each
do while
“while” loop is used to run a block of code as long as given condition is true. If
condition is false at any time, loop should be terminated and controller will go to next
statement which is outside of loop.
“do-while” loop is used to run a block of code as long as given condition is true. but
here, block of code will be executed at least once when given condition is false initially.
while loop – pre condition
do-while loop – post condition
“for” loop is used to run a block of code for given no: of times.
for (initialization; condition; increment/decrement;)
{
18 | P a g e JAVA NOTES
----------
----------
}
Example 1:
for(int i=1; i<=10; i++)
{
--------
--------
} //10 iterations
Example 2:
for(int i=0; i<10; i++)
{
--------
--------
} //10 times
Example 3:
for(int i=1; i>=10; i--)
{
--------
--------
} //10 times
Example 4:
for(int i=0; i<10; i=i+2)
{
--------
--------
} //5 iterations
Example 5:
for(int i=0; ; i++)
{
--------
--------
} //infinite loop
Example 6:
Exercises:
1). Find Odd or Even Number:
A number divided by 2 (reminder is 0) called as Even.
A number not divided by 2 (reminder is 1) called as Odd.
19 | P a g e JAVA NOTES
Example:
public class OddEvenFinder
{
public static void main(String[] args)
{
int x=567487; // initialize the variable value
if(x%2==0) // condition for find Odd or Even Number
{
System.out.println("x Even Number");
}
else
{
System.out.println("x odd number");
}
}
}
2). Find Prime Number:
A number divided by 1 and itself only, means that number can’t be divided by 2
to that (number – 1).
Example:
x is prime when x is not divided by 2 to (x-1).
7 is prime number because 7 not divided by 2, 3, 4, 5 and 6 (7-1).
Program:
public class PrimeNumberFinder
{
public static void main(String[] args)
{
// take a number from key board
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Number ");
int x = sc.nextInt();
sc.close();
// check for Prime Number or Not
int flag = 0;
for (int i=2;i<x;i++) // 2 to x-1
{
if (x%i==0)
{
flag = 1;
break;
}
} // loop will be terminated either "at end" of because of "break"
if (flag==0)
20 | P a g e JAVA NOTES
{
System.out.printf("%d is a prime number",x);
}
else
{
System.out.printf("%d is not a prime number",x);
}
}
}
22 | P a g e JAVA NOTES
6). Find Factorial on Numbers:
Program:
public class FactorialOnNumbers
{
public static void main(String[] args)
{
//Take a number from keyboard
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Number");
int n = sc.nextInt();
sc.close();
// Calculate Factorial (4! is 4*3*2*1)
long f = 1; //initial o/p is 0 for sum and 1 for multiplications
for (int i=n;i>=1;i--) // n to 1
{
f = f * i;
}
System.out.printf("%d factorial value is %d",n,f);
}
}
7). Print Fibonacci series up to given limit:
Program:
public class FibonacciSeries
{
public static void main(String[] args)
{
// Take the Limit from Keyboard
Scanner sc = new Scanner(System.in);
System.out.println("Enter Limit");
int l = sc.nextInt();
sc.close();
// form the series
int x = 0;
int y = 1;
System.out.printf("%d %d",x,y);
do
{
int z = x+y;
System.out.printf(" %d",z);
x = y;
23 | P a g e JAVA NOTES
y = z;
}while((x+y)<=1);
}
}
8). Find number of digits in given number:
Program:
public class FindCountOfDigitsInGivenNumber
{
public static void main(String[] args)
{
// take a number from keyboard
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Number");
int n = sc.nextInt();
sc.close();
// find count digits in given number
int count = 0; // initial count is "0"
long temp = n; // declaring number as temp
while(temp!=0) // if temp not equal to "0"
{
temp = temp/10; // cut last digit
count++; // repeat above statement
}
System.out.printf("%d has %d digits",n,count);
System.out.printf(+n+" has "+count+" digits"); //Concatenation
}
}
9). Find Sum of digits of a given number:
public class SumOfDigitsOfGivenNumber
{
public static void main(String[] args)
{
// take a number from keyboard
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Number");
long number = sc.nextLong();
sc.close();
// sum of digits of given number
int sum = 0; // initial Sum is '0'
long temporary=number;
// copy original number into temporary number
24 | P a g e JAVA NOTES
while(temporary!=0)
// when temporary value not equals to '0' loop will generate
{
int lastdigit=(int)(temporary%10); //get last digit
sum = sum + lastdigit; //add that last digit to sum
temporary =(long)temporary/10;
// cut that last digit from number
} // loop will terminated when temporary value gets '0'
System.out.printf(sum+" is the sum of digits of "+number,sum,number);
}
}
10). Find Sum of digits of a given number until that sum become a single digit:
26 | P a g e JAVA NOTES
14). binary to decimal:
Pending program
Note 1: “Math” class is related to “java.lang” package in JDK. “pow()” is a static
method in “Math” class.
If we are interested, the below method is alternative to math.pow() to use in scripts:
Note 3:
Pending program
16). Display pattern:
27 | P a g e JAVA NOTES
17).
18).
28 | P a g e JAVA NOTES
19).
20).
21).
29 | P a g e JAVA NOTES
22).
23).
30 | P a g e JAVA NOTES
Logic for 23:
24).
25).
26).
31 | P a g e JAVA NOTES
Side by side program
27).
28).
29).
32 | P a g e JAVA NOTES
30). Draw a box:
//upper line // edges
33 | P a g e JAVA NOTES
31). Draw a Ship:
From the above example, “String” class object can display “value” because it
behaves like variable. But any other class’s object can display details of
corresponding class, package and allocated memory.
Ex:
34 | P a g e JAVA NOTES
From the above examples, Hashing is the process of mapping the data to some
representative integer value using the concept of hashing algorithms.
In Java, a hash code is an integer value that is linked with each object. If two
objects are equal, then their hash code will be identical. If there are two
different objects at two different memory locations, they will have strictly
different hashcodes.
(Or)
In general,
1) When we create an object to any class, object gets memory in heap area.
35 | P a g e JAVA NOTES
2) But when we create a String variable/ object, it gets memory in String
Pool in heap area.
Here both objects refer same memory because values are same and String is
immutable.
36 | P a g e JAVA NOTES