Friday, November 2, 2018

Core Java Concepts

Overview about Java
Java programming language was originally developed by Sun Microsystems, which was
initiated by James Gosling and released in 1995 as core component of Sun Microsystems’s
Java platform (Java 1.0 [J2SE]).
Sun Microsystems has renamed the new J2 versions as Java SE, Java EE and Java ME
respectively. Java is guaranteed to be Write Once, Run Anywhere
Java is:
Object Oriented : In java everything is an Object. Java can be easily extended since it is
based on the Object model.
Platform independent: Unlike many other programming languages including C and C++
when Java is compiled, it is not compiled into platform specific machine, rather into
platform independent byte code. This byte code is distributed over the web and
interpreted by virtual Machine (JVM) on whichever platform it is being run.
Simple: Java is designed to be easy to learn. If you understand the basic concept of OOP
java would be easy to master.
Secure: With Java's secure feature it enables to develop virus-free, tamper-free systems.
Authentication techniques are based on public-key encryption.
Architectural- neutral :Java compiler generates an architecture-neutral object file format
which makes the compiled code to be executable on many processors, with the presence
Java runtime system.
Portable: being architectural neutral and having no implementation dependent aspects of
the specification makes Java portable. Compiler and Java is written in ANSI C with a clean
portability boundary which is a POSIX subset.
Robust: Java makes an effort to eliminate error prone situations by emphasizing mainly
on
compile time error checking and runtime checking.
Multi-threaded: With Java's multi-threaded feature it is possible to write programs that
can do many tasks simultaneously. This design feature allows developers to construct
smoothly running interactive applications.
Interpreted: Java byte code is translated on the fly to native machine instructions and is
not stored anywhere. The development process is more rapid and analytical since the
linking is an incremental and light weight process.
High Performance: With the use of Just-In-Time compilers Java enables high performance.

Distributed :Java is designed for the distributed environment of the internet.
Dynamic : Java is considered to be more dynamic than C or C++ since it is designed to
adapt to an evolving environment. Java programs can carry extensive amount of run-time
information that can be used to verify and resolve accesses to objects on run-time.
History of Java:
James Gosling initiated the Java language project in June 1991 for use in one of his
many set-top box projects. The language, initially called Oak after an oak tree that
stood outside Gosling's office, also went by the name Green and ended up later renamed
as Java, from a list of random words.
Sun released the first public implementation as Java 1.0 in 1995. It promised Write Once,
Run Anywhere (WORA), providing no-cost run-times on popular platforms.
On 13 November 2006, Sun released much of Java as free and open source software
under the terms of the GNU General Public License (GPL).
On 8 May 2007 Sun finished the process, making all of Java's core code free and
open-source, aside from a small portion of code to which Sun did not hold the copyright.
Setup Environment Variables:
Follow the steps
1. Right click on my computer icon of desktop of ur computer.
2. Select properties.
3. Select "advanced" tab
4. Click on Environment Variables button.
5. Focus on System variables
6. Focus of Path Variable
7. Click on Edit.Here variable is a pair of values name and value.
8. PATH: c:; d:;e:;C:\Program Files\Java\jdk1.6.0_14\bin;

IN C Language:
In Java


Execution Process of Java in all platforms:
Creating Java Application:
  1. creating source code
To do this we can use any text editors.
We have to form source and must save it to a file .java for
EX:- MyApp.java
  1. generate byte code by compiling source using javac.exe
To do this
javac <sourcefilename>
EX:- javac MyApp.java
This JAVAC Compiler generates MyApp.class(byteCode).
  1. Interpretation using java.exe
Once we generated .class files we need to interpret them to platform specific code (binary code or target code or native code).This is done using Java.exe
EX:- java <classfilename>
Java MyApp
It generated .exe file for the platform where ur working.
Class:
A class is a way of creating a new data type. Once it is created we can use them for creation of objects. it is also allows programmers to group data and functionality together.
This is done using "Class" keyword.
Main Class:
It is an important section for all programs. it is defined as a class which has main method is called main-class.
Syntax main method:
public static void main(String[] args)
{
------
------
}

Example 1:
Public  class sample {
public static void main (string args[])
{
System.out.println(“ Welcome To QAPlanet ”);
}
}
Data Types:
A data type is a keyword and it is used for performing three things.
They are
1. It is used for allocation of fixed size memory.
2. It is also used for specifying type of information to be stored in that memory.
3. It is also used for giving name (variable) to that memory for further usage throughout the program.
Example:
Student - name - "xyz“- char name [3] ="xyz"; (3bytes)
Age - 25        - int age = 25; (2bytes)
rollno - 1000    - int r = 1000;
Height - 5.4      - float h=5.4; (4bytes)
- A value type is used for declaration of values directly.
Value types provided by Java are
Boolean    - 1-bit
Byte    - 8-bit
short     - 16 - bit
int   - 32-bit
long      - 64-bit
float     - 4-bytes
double    - 8-bytes.
char    - 1-byte
String - depends up on no. of characters.





Operators:
We are using to perform Assignment, Arithmetic, Comparisons and logical operations.
The Simple Assignment Operator:
Assignment operator "=".
The Arithmetic Operators:
+ Additive operator
- Subtraction operator
* Multiplication operator
/ Division operator
% remainder operator
The Unary Operators
+ Unary plus operator; indicates positive value (numbers are positive without this, however)
- Unary minus operator; negates an expression
++ Increment operator; increments a value by 1
-- Decrement operator; decrements a value by 1
! Logical complement operator; inverts the value of a Boolean
The Equality and Relational Operators
== Equal to
!= not equal to
> Greater than
>= greater than or equal to
< Less than
<= less than or equal to

The Conditional Operators
&& Conditional-AND
||    Conditional-OR
?:   Ternary (shorthand for if-then-else statement)
Type Comparison Operator
instanceof               Compares an object to a specified type
Bitwise and Bit Shift Operators
~ Unary bitwise complement
<< Signed left shift
>> Signed right shift
>>> Unsigned right shift
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise inclusive OR







public class operators {
public static void main(String args[]){
// for addition
int a=1+2;
System.out.println(a);

// For Concatenation
String firstString = "This is";
String secondString = " a concatenated string.";
String thirdString = firstString+secondString;
System.out.println(thirdString);

// Unary Operators
int result = +1; // result is now 1
System.out.println(result);
result--;  // result is now 0
System.out.println(result);
result++; // result is now 1
System.out.println(result);
result = -result; // result is now -1
System.out.println(result);
boolean success = false;
System.out.println(success); // false
System.out.println(!success); // true

// Increment and decrement operators
int i = 3;
i++;
System.out.println(i); // "4"
++i;
System.out.println(i); // "5"
System.out.println(++i); // "6"
System.out.println(i++); // "6"
System.out.println(i); // "7"

//Comparison Demo
int value1 = 1;
int value2 = 2;
if(value1 == value2) System.out.println("value1 == value2");
if(value1 != value2) System.out.println("value1 != value2");
if(value1 > value2) System.out.println("value1 > value2");
if(value1 < value2) System.out.println("value1 < value2");
if(value1 <= value2) System.out.println("value1 <= value2");

// Conditional operators
int val1 = 1;
int val2 = 2;
if((val1 == 1) && (val2 == 2))
System.out.println("val1 is 1 AND val2 is 2");
if((val1 == 1) || (val2 == 1))
System.out.println("val1 is 1 OR val2 is 1");
//Another conditional operator is ?:
int v1 = 1;
int v2 = 2;
int result1;
boolean someCondition = true;
result1 = someCondition ? v1 : v2;
System.out.println(result1);
}
}


Arrays:
Array is collection of similar elements
Array starts from zero
Declaring a Variable to Refer to an Array
int[] anArray;
Similarly, you can declare arrays of other types:
byte[] anArrayOfBytes;
short[] anArrayOfShorts;
long[] anArrayOfLongs;
float[] anArrayOfFloats;
double[] anArrayOfDoubles;
boolean[] anArrayOfBooleans;
char[] anArrayOfChars;
String[] anArrayOfStrings;

Example:
public class array {
public static void main(String[] args) {

int[] anArray;              // declares an array of integers

anArray = new int[4];      // allocates memory for 4 integers

anArray[0] = 100;
anArray[1] = 200;
anArray[2] = 300;
anArray[3] = 400;

System.out.println("Element at index 0: " + anArray[0]);
System.out.println("Element at index 1: " + anArray[1]);
System.out.println("Element at index 2: " + anArray[2]);
System.out.println("Element at index 3: " + anArray[3]);
System.out.println("Length of Array:="+anArray.length);
}

}

Multidimensional Arrays:

class MultiDimArrayDemo {
public static void main(String[] args)
{
String[][] names = {{"Mr. ", "Mrs. ", "Ms. "}, {"Smith", "Jones"}};
System.out.println(names[0][0] + names[1][0]); //Mr. Smith
System.out.println(names[0][2] + names[1][1]); //Ms. Jones
}
}

Control structure:
1.Conditional statements
2.Looping statements
3.Branching statements

Conditional statements:
If, if -else, if-else-if, Nested If, switch

public class conditions {
public static void main(String[] args){
// if and if else condition
int a=10;
if(a%2==0)
{
System.out.println("Value is even number");
}
else
{
System.out.println("Value is odd number");
}

//if - else if condition
int b=1;
if (b<10)
{
System.out.println("Value is less than 10");
}
else if (b<20)
{
System.out.println("Value is less than 20");
}
else
{
System.out.println("Value is greater than 20");
}

//Nested if conditions
int c=1;
if (c<10)
{
if (c<5)
{
System.out.println("Value is less than 5");
}
else
{
System.out.println("Value is less than 10");
}
}
else
{
System.out.println("Value is greater than 10");
}

// Switch
int month = 8;
String monthString;
switch (month) {
case 1:
monthString = "January";
break;
case 2:
monthString = "February";
break;
case 3:
monthString = "March";
break;
case 4:
monthString = "April";
break;
case 5:
monthString = "May";
break;
case 6:
monthString = "June";
break;
case 7:
monthString = "July";
break;
case 8:
monthString = "August";
break;
case 9:
monthString = "September";
break;
case 10:
monthString = "October";
break;
case 11:
monthString = "November";
break;
case 12:
monthString = "December";
break;
default:
monthString = "Invalid month";
break;
}
System.out.println(monthString);

}
}




Looping Conditions:
for, while, do-while
public class Loops {

//While loops
public static void main(String[] args){
int count = 1;
while (count < 11)
{
System.out.println("Count is: " + count);
count++;
}

/*//////// /*
*
The Java programming language also provides a do-while statement

do {
statement(s)
} while (expression);

*
*///////////////////////////////
int count1 = 1;
do {
System.out.println("Count is: " + count1);
count1++;
} while (count1 <= 11);

// For loop statemnent
/* /************************************
for (initialization; termination; increment) {

statement(s)
}
************************************************/
for(int i=1; i<11; i++){
System.out.println("Count is: " + i);
}

// For in loops (we are using for arrays)
int[] numbers = {1,2,3,4,5,6,7,8,9,10};

for (int item : numbers) {
System.out.println("Count is: " + item);
}

}

}

Branching statements:
  • break, continue, return
public class Branching {
public static void main(String[] args) {
// break command
int[] arrayOfInts = { 32, 87, 3, 589, 12, 1076,2000, 8, 622, 127 };
int searchfor = 12;

int i;
boolean foundIt = false;

for (i = 0; i < arrayOfInts.length; i++) {
if (arrayOfInts[i] == searchfor) {
foundIt = true;
break;
}
}

if (foundIt) {
System.out.println("Found " + searchfor + " at index " + i);
} else {
System.out.println(searchfor + " not in the array");
}

// Continue
String searchMe = "peter piper picked a peck of pickled peppers";
int max = searchMe.length();
int numPs = 0;

for (int j = 0; j < max; j++) {
//interested only in p's
if (searchMe.charAt(j) != 'p')
continue;

//process p's
numPs++;
}
System.out.println("Found " + numPs + " p's in the string.");

}

}

TestNG - Can i use the 2 different data providers to same @test methods in TestNG?

public Object [][] dp1 () { return new Object [][] { new Object [] { "a" , "b" }, new Obje...