Java Introduction
Java Introduction
Software
Eg:
C, C++, Java, .Net , ERP package
and others
Operating System:
Application Software
Front End Software Back End Software
These are to interact with end users, It is Serverside Software
it collects data from end users and
Application 2) Database
stores into backend
Application is to perform serverside /
Eg:
backend transactions
Languages, Packages ERPs
Database used to store and maitain
data
Eg:
Databases and files , HDFS ( Bigdata
storage )
Software Languages
================
Eg:
C, C++, Java, Python, HTML, .net C#, Scala..
Compliers and interpreters are used to convert form high level languages
into low level language ( processor understandable language )
Interpreter based languages don’t Every program converts into exe file
8
have exe file concept
Open Source, it can be modified It is not an open source , it is exe
9
any time based , it’s code can not be modified
Ans : No
JAVA
Features of Java :
1) It is an independent of platform.
3) It is a scalable language.
Platform:
Any hardware or software environment in which a program runs is known
as a Platform.
Since Java has its own runtime environment (JRE) and API, it is called
Platform.
1) It is an independent of platform
Java application runs any operating system and any Hardware
3) It is a scalable language
Easy to use without disturbing the existing java functionality
Eg:
Any exe file prepared on windows will not execute on Linux and vice versa
Java byte code files not an exe file.
Java Editions:
4 Java FX:
JSE Features:
4) Collections
7) Networking.
8) Multi-threading.
9) Applets for Web applications -- >It was outdated concept.
10) AWT ( Abstract Windowing Tool Kit ) for GUI Applications --> It was
outdated concept.
JEE Features :-
1) Servlets
2) JSP
9) XML Parser
1) Struts Framework
2) Spring Framework
3) Hibernate Framework.
Java Installation :
Installing Java
Copy JDK1.8 into C-Drive and rigth click run as Administrator yes
next next close
1) JAVA_HOME
2) PATH
Win-10
Setting path :
$gedit ~/.bashrc
go to end of .bashrc file and provide following PATH
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-i386
Ans:
set is command
set JAVA_HOME
What is purpose of java PATH setting ?
Ans:
Eclipse setup:
Take the eclipse zip ( archive ) and place into C-dirve rt.click and select
extract here Open Eclipse folder right click on eclipse icon send to
desk top created short cut on desk for eclipse.
On launching eclipse, it check for the JAVA_HOME, then integrates jdk with
eclise.
Java Compiler, JRE, JVM are integrated to eclipse automatically, when PATH
variable is defined in the OS.
Create Workspace
Create a Project
Create a Package
Create a Class
Create Methods
Step1:
Create Workspace
Goto Desk top and double click on Eclipse Icon, then Launch eclipse
Select Work Space location ( browse ) and provide the name , D:\test\
SampleWS
Create Project
Rt.Click on Project Explorer area new project java project next
provide project name ( test)
execution environment : JavaSE1.8
Finish
Step3:
Create a package
com Company
org Organization
Eg:
com.angelprints.ecommerce.inventory
Step4:
Create a class
Provide the class name : Test ( class name should start with upper case
alphabet )
package com.nrit.mnrao.test;
System.out.println("Hello World");
At Console O/p:
Hello World
/*
Developed On :
Developed By :
Modified On :
Modified By :
Reason to Modify :
Description :
import java.lang.String ;
import java.lang.System ;
Name of program file should be same as the class name in which main
method is defined.
Commenting :
/* multiple lines
commenting
in java
*/
Q. what is a package ?
ans:
import java.lang.* ,
import java.lang.String ;
import java.lang.System ;
Note :
Naming Conditions
Naming Conventions
These are as per the coding standards .
identifiers:
=======
class name,
interface name,
variable name,
object name,
method name
These are as per coding standards for easy understanding the code.
Java follows camel case syntax for naming the class, interface, method
and variable.
public keyword is an access specifier which represents visibility, it means
it is visible to all.
No two classes are public in the same program file.
2) if name contains multiple words, then first word starts with lowercase
and next words starts with uppercase alphabet
e.g.
getData(), setData(), getEmpInformation()
actionPerformed(), firstName(),
setEmpDeptName()
getDatabaseConnection()
actionPerformed()
getRateOfInterest()
getMinRateOfInterest()
getMaxRateOfInterest()
main() with private access specifier can be compled but can not execute
main() can be compiled with any access specifier but to execute it should
be public
void is the return type of the method, it means it doesn't return any
value.
main() can be compiled with any return type but to execute return should
be void.
if don't have String [] args, the it will be treated as user define method ,
not JVM main()
as pepr JVM. main() syntax should be as below
public static void main(String [] args);
String[] args is used for command line argument. We will learn it later.
println () inserts a new line char at the end of the data, where as print()
does not.
A program file can have multiple classes but only one class must be public
class Sample
{
public static void main(String [] args )
{
System.out.println("Hello NRIT");
}
}
Ans:
Yes , an empty .java program file can be compiled but does not
generate .class file
eg:
ContractEmployee, ReserveBankOfIndia,
MonthlySalesReport
interface
Same as class name
name
1) name should start with lower case alphabet
2) if name contains multiple words, then first word is a lower
method name
case and next coming words start with upper case
package should be in lowercase letter e.g. java, lang, sql, util etc.
name com.sritech.mnrao.test
constants should be in uppercase letter. e.g. RED, YELLOW,
name MAX_PRIORITY etc.
Overview of JVM
Highlevel Architecture :
Class Loader : Loading .class file into memory
Interpreter :
It coverts java byte code to binary code and prepares as per the current
platform (Hardware ) runtime environment.
JIT :
Interview Questions:
Ans:
It is a platform, where we can execute application programs and
It is an interface between Hardware and Application software.
Ans:
exe file is a prepared file for specific OS. It contains binary code.
exe file contains, prog
ram code, application software libraries, OS libraries and hardware
information
exe file is a OS dependent.
3) can we create exe file in java ?
Ans :
No we can't create exe file. In java every thing is .class file, it is not
an exe file
Ans;
.class file is byte code file, which contains only java program code
and Java libraries.
It don’t have any OS library and it is independent of platform
Ans;
it is independent of Platform since it does not contain any OS
libraries and Hardware information.
Since Java .class file dynamically linking OS library by the JVM , it is
an independent
Ans:
exe file contains binary code and .class file contains byte code
exe file is prepared file for specific OS and .class is not prepared file.
After moving .class file to OS, then preparation starts as per current
OS.
Exe file contains OS library but .class file don’t have any OS Library
JVM, JRE and JDK are platform dependent because configuration of each
OS differs.
But, Java is platform independent.
JDK
Development Tools
JRE javac, java, javap, javadoc, jar,
appletviewer, rmic, jps and etc )
Java
Packages
JVM
Classes
( Library )
Loads code
Verifies code
Executes code
Provides runtime environment
Ans:
Just-In-Time (JIT) compiler is a feature of the run-time interpreter.
It prepares the binary code ( generated by Interpreter ) as per the
current OS Specifications.
Ans: yes.
Ans:
java application build can be moved from one OS to another OS with
out any changes in the application build .
Ans : Yes
Ans:
No
A class must be public which name is same as File name.
A Java-source file with zero class-declarations will not result in any .class
files
Java is a net based product, to accept all keyboard sets across the
universe it is designed with 2 bytes.
Char with 2 bytes can accept the value ranging from 0 to 65535 ( many
keyboard sets )
1 byte = 8 bits
1 MB ( 106 ) = 1024 KB
1 GB ( 109) = 1024 MB
1 TB ( 1012) = 1024 GB
1 PB ( 1015 ) = 1024 TB
1 EB ( 1018 ) = 1024 PB
1 ZB ( 1021 ) = 1024 EB
TB to ZB is a Big Data.
Hadoop is frame work to store and Process Bigdata.
to store bigdata
using as backend
to process data
byte b = 10;
2 10
2 5 -- 0
2 2 -- 1
2 1 -- 0
0 -- 1
1010
Last bit is reserved for sign and other bits for data.
0 0 0 0 1 0 1 0
1 + 2 + 4 + 8 + 16 + 32 + 64 = 127
4 bytes of memory
first bit
27 26 25 24 23 22 21
20
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
0 1 1 1 1 1 1 1
214 213 212 211 210 29
28
Last bit is for sign
1) ASCII char --> American Standard Code for Information and Interchange
First bit
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
First bit
27 26 25 24 23 22 21 20
1 1 1 1 1 1 1 1
If character type is ASCII char it can support char ranging from 0 to 255
only
If char size is only 1 byte it supports only USA, English key board , not
other keyboard sets.
Any key board set released in the future that should be started from 256.
Sun Micro systems has developed standard for every keyboard with max
only 256 keys and same thing was Registered.
If it is ASCII char , it can take only USA, English , it does not accept other
keyboard sets.
0 to 255 Registered for USA, English.
If size of the char is 2 bytes , it can take many keyboard sets across the
world wide.
To support different languages in the Universe char designed with 2 bytes.
O/P:
Variable :
What is variable ?
it is an identifier
variable naming :
============;
1) Naming Conditions
2) Naming Conventions
1) Naming Conditions
identifiers:
=========
class name,
interface name,
variable name,
object name,
method name
2) Naming Conventions
1st word start with lowercase , and next word start with upper case
alphabet
Eg:
empDeptName;
empSalary;
Declaration of a variable.
data_type variable_name;
eg :
int empNum;
String empName;
double empSalary;
String empDeptName;
String empGender;
int empAge
empNum = 1001;
empName = "mnrao";
empSalary = 50505.50;
empDeptName ="it";
empGender = "male";
empAge = 35;
Memory Management by JVM
Stack Memory
Shared Memory
Heap Memory
Static Memory
Global Memory
Text Area
Method Area
Higher order memory space :
To store all command line paramters and OS environment variable.
// declaring variables
int empNum;
String empName;
empAge
35
double empSalary;
male
String empDeptName;
it
String empGender;
int empAge;
50505.50
empNum = 1001;
1001
empName = "mnrao";
empSalary = 50505.50;
empDeptName ="it";
empGender = "male";
empAge = 35;
System.out.println(empNum);
System.out.println(empName);
System.out.println(empSalary);
System.out.println(empDeptName);
System.out.println(empGender);
System.out.println(empAge);
}
}
Once control come out of main method, stack becomes empty ( all
varaibles clears from stack )
Initialization of variables:
int empAge = 35 ;
program for initialization
int empAge = 35 ;
System.out.println(empNum);
System.out.println(empName);
System.out.println(empSalary);
System.out.println(empDeptName);
System.out.println(empGender);
System.out.println(empAge);
}
}
package com.durga.mnrao.abc;
Stack
public class Test {
female
String empDeptName = "admin"; empGender
sales
String empGender = "male"; empDeptName
60607.60
int empAge = 35 ;
System.out.println(empNum);
1002
System.out.println(empName);
System.out.println(empSalary);
System.out.println(empDeptName);
System.out.println(empGender);
System.out.println(empAge);
// changing values
empNum = 1002;
empName = "scott";
empSalary = 60607.60;
empDeptName = "sales";
empGender = "female";
empAge = 30;
System.out.println(empNum);
System.out.println(empName);
System.out.println(empSalary);
System.out.println(empDeptName);
System.out.println(empGender);
System.out.println(empAge);
}
}
+ concatenation operator
a+b
package com.durga.mnrao.test;
// initialization of variables
int empAge = 35 ;
empNum = 1002;
empName = "scott";
empSalary = 60607.60;
empDeptName = "sales";
empGender = "female";
empAge = 30;
System.out.println("after changing value are ");
}
}
int c = a*b;
int a = 10; 20 20 10
int b = 20;
int temp = a;
a = b;
b = temp;
}
}
Before swapping
a=10 b=20
After swapping
a=20 b=10
==============
Reading data from keyboard
There are many ways to read data from the keyboard. For example:
Scanner
InputStreamReader
Console
DataInputStream etc.
BufferedReader
At this stage, we can focus only on Scanner and others will be discussed in
I/O streams.
The Java Scanner class breaks the input into tokens using a delimiter that
is whitespace by default.
It provides many methods to read and parse various primitive values.
All methods will read data upto next token ( space or tab or \n, which is
coming fisrt), except , nextLine()
nextLine() : it will read input data upto eneter key including space and tab.
It will read any type of data.
next() : It will read input data up to space or tab or new line char, which
is coming first. It will read any type of data.
All methods, except nextLine() and next(), can read only numeric data.
Program to read different type of data from keyboard
import java.util.Scanner;
byte b = sc.nextByte();
short s = sc.nextShort();
int i = sc.nextInt();
System.out.println("int varible i = "+i);
long l = sc.nextLong();
float f = sc.nextFloat();
double d = sc.nextDouble();
sc.close();
Eg:
package com.durga.mnrao.xyz;
import java.util.Scanner;
}
}
Type Casting:
1) Implicit Casting
eg1:
byte b=10;
short s = b ; --> valid
eg2:
int a=10;
long b=a; --> valid
eg3:
float a=10.5f;
double b=a; --> valid
Eg:
int i = 100;
float f = i ;
eg:
int a = 1000;
double d = a;
byte ===> short ===> int ===> long ==>float ===> double
eg1:
short s = 10;
byte b = (byte) s;
eg2:
int i = 100;
eg3:
double d = 10.5;
float f = (float) d;
Eg:
short a=10;
byte b = a; --> invalid, since size of variable “ a” is 2 bytes , which
is more than size of variable “b” ( 1 byte )
short a=10;
short a=130;
byte b = (byte ) a ; --> it compiles but at run time loss of data. It results in
unexpected data ( i.e leads to bugs )
double d = 10.5;
int i = (int) d;
long l = (long) d ;
float f= 10.5 ; --> invalid, since in java real numbers by default treats as
double.
(or)
Division ( / ) operation:
int a =10;
float x = a / 3 ;
reason in division, both operands are int type then result int.
to get real number, at leat one operand must be float / double
int a =10;
float x = a / 3f ;
result is 3.3333333
eg:
int a =10;
int b = 3;
float x = a / b ;
reason in division, both operands are int type then result int.
float x = (float) a / b ;
(or)
float x = a / (float) b ;
Java Operators
Binary Operators:
2) Relational Operators
3) Logical Operators
4) Assignment Operator
6) Bitwise operators
Operator Task
+ Addition
- Subtraction
* Multiplication
/ Division
% Modula’s
( remainder )
Eg:
10 % 3 =1
3%10 = 3
9%10 = 9
50%51=50
Relational Operators:
Operator Task
> Greater than
>= Greater than or equal
to
< Less than
<= Less than or equals to
== Is equal to
!= Not equal to
== --> comparison
= --> assignment.
These operators are to compare the values and returns either true or
false ( Boolean values )
Eg:
int a = 10;
int b = 5;
Logical Operators:
Operator Task
&& And
|| Or
! Not
Using ( | | ) operator:
int a = sc.nextInt();
int b = sc.nextInt();
int c = a + b;
System.out.println("addition = "+c);
c = a - b;
System.out.println("subtraction = "+c);
c = a * b;
System.out.println("Multiplication = "+c);
int a = 10;
a = a + 5; --> a +=5 ;
a = a - 5; --> a -=5;
a = a * 5; -->a *= 5;
a = a / 5 ; --> a/=5;
Unary Operator:
1) Unary Minus ( - )
2) Increment Operator ( ++ )
3) Decrement Operator ( -- )
Unary Minus:
Eg:
int a=10;
Increment Operator ( ++ ) :
Decrement operator ( - - ):
Increment Operator ( ++ ) :
Pre – increment Post – increment
1) syntax : 1) syntax :
++ variable; variable++;
2)It increase value of it's Operand by 1 2)It increase value of it's Operand by 1 after
before executing statement executing statement
3) eg : 3) eg :
int a=10; int a=10;
int b = ++a: --> a=11, int b = a++: -->b=10, a=11
b=11
int a=10;
int b;
int c;
int d;
b=++a;
c=b++;
d=++c;
System.out.println (a+"\t"+b+"\t"+c+"\t"+d);
}
}
O/p : 11 12 12 12
b=a++;
c=++b;
d=c++;
b=d++;
a=++c;
System.out.println (a+"\t"+b+"\t"+c+"\t"+d);
}
}
O/P : 13 11 13 12
public class Test {
public static void main(String[] args)
{
int a=10;
int b;
int c;
int d;
d=++a;
b=d++;
a=++b;
c=d++;
d=++c;
System.out.println (a+"\t"+b+"\t"+c+"\t"+d);
}
}
O/P :
12 12 13 13
Decrement Operator ( - - ) :
2)It decreases value of it's Operand by 2)It decreases value of it's Operand
1 before executing statement by 1 after executing statement
3) eg : 3) eg :
int a=10; int a=10;
int b = --a: --> a=9, b=9 int b = a--: -->b=10, a=9
b=--a;
c=b++;
d=--c;
a=++b;
d=a--;
System.out.println (a+"\t"+b+"\t"+c+"\t"+d);
}
}
O/P :
10 11 8 11
d = a--;
b = ++d;
c = a++;
a = --b;
d = c++;
10 10 10 9