1 Basics
1 Basics
What is Java?
1) Standalone Application
2) Web Application
3) Enterprise Application
4) Mobile Application
Desktop GUI Applications
Java support for web development through Servlet, JSP, and Struts.
Also known as a server-side programming language.
frameworks Spring, Hibernate, Spring Boot, used for developing web-
based applications.
LinkedIn, AliExpress, web.archive.org, IRCTC, etc. are the popular
websites that are written using Java programming language.
Enterprise Applications of Java
Java SE’s API offers the Java programming language’s core functionality.
Defines all the basis of type and object to high-level classes.
Used for networking, security, database access, graphical user interface
(GUI) development.
Java Platform, Enterprise Edition (Java EE):
• Offers an API and runtime environment for developing and running highly
scalable, large-scale, and secure network applications.
• Is an abstract machine.
• The JVM doesn't understand normal English lang, that's why we compile our
*.java files to obtain *.class files that contain the bytecodes understandable by the
JVM.
Important features of JVM are
• JVM runs the program by using libraries and files given by the Java Runtime
Environment.
• JDK and JRE both contain Java Virtual Machine.
• It can execute the java program line by line hence it is also called an interpreter.
• JVM is easily customizable for example, we can allocate minimum and maximum
memory to it.
• It is independent of hardware and the operating system. So, we can write a java
program once and run it anywhere.
The JVM performs following operation:
• Loads code
• Verifies code
• Executes code
• Stack : It holds local variables and partial results, and plays a part in method
• Native method stack : It contains all the native methods used in the application.
• Native Method Interface : Native method interface gives an interface between java
code and native code during execution.
• Native Method Libraries : Native Libraries consist of files required for the
execution of native code.
native method
• Access system or hardware resources that are only reachable from the other
language
and applets.
Interpreter translates just one statement of the program Compiler scans the entire program and translates the
at a time into machine code. whole of it into machine code at once.
An interpreter takes very less time to analyze the A compiler takes a lot of time to analyze the source
source code. However, the overall time to execute the code. However, the overall time taken to execute the
process is much slower. process is much faster.
It does not convert source code into object code It converts the source code into object code.
instead it scans it line by line
Keeps translating the program continuously till the A compiler generates the error message only after it
first error is confronted. If any error is spotted, it stops scans the complete program and hence debugging is
working and hence debugging becomes easy. relatively harder while working with a compiler.
Interpreters are used by programming languages like Compliers are used by programming languages like C
Ruby and Python for example. and C++ for example.
Java Code Conventions
Code /Naming convention
• By using standard Java naming conventions, we make our code easier to read
• It indicates that less time is spent to figure out what the code does.
Classes and interfaces
• First letter must be lowercase, and then normal camelCase rules are used.
• First letter must be lowercase, and then normal camelCase rules are used.
buttonHeight.
Constants
1. local variable
2. instance variable
3. static variable
1. Local Variable:
• Variable declared inside the body of the method.
2. Instance Variable
•Variable declared inside the class but outside the body of the method.
3. Static variable
• Variable which is declared as static is called static variable.
• It cannot be local
• Memory allocation for static variable happens only once when the class is
loaded in the memory
Example to understand the types of variables in java
class A
{
int data=50; //instance variable
static int m=100;
//static variable
void method()
{
int n=90; //local variable
}
}
Java Variable Example: Widening
class Simple{
public static void main(String[] args)
{
int a=10;
float f=a;
System.out.println(a); OUTPUT:
System.out.println(f); 10
10.0
}}
Java Variable Example: Narrowing
class Simple {
public static void main(String[] args) {
float f=10.5f;
int a=f;
int a=(int)f;
OUTPUT:
System.out.println(f);
System.out.println(a); 10.5
10
}}
Data Types in Java
Integer
byte :
short :
long :
• It is 8 bytes(64-bits) integer data type.
• Value range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
• Default value zero. example:
• long l=100012;
// byte type
byte b = 20;
System.out.println("b= "+b);
// short type
short s = 20;
System.out.println("s= "+s);
// int type
int i = 20;
System.out.println("i= "+i);
// long type
long l = 20;
System.out.println("l= "+l);
Floating-Point Number
This group includes float, double
float :
• It is 4 bytes(32-bits) float data type.
double :
• It is 8 bytes(64-bits) float data type.
• Default value 0.0d.
• example: double db=11.123;
Characters
boolean t = true;
System.out.println(t);
boolean f = false;
System.out.println(f);
} }
Type casting in java
Type Casting in Java
• Process of converting the value of one data type (int, float, double, etc.)
Into another data type is known as type casting.
There are two types of casting:
// i % 256
b = (byte)i;
// d % 256
b = (byte)d;
char a = '5';
char b = 'c';
int num1 = a;
int num2 = b;
System.out.println(num1); // 53
System.out.println(num2); // 99
char to int using getNumericValue() method
char a = '5';
char b = '9';
System.out.println(num1); // 5
System.out.println(num2); // 9
char to int using parseInt() method
char a = '5';
char b = '9';
// Use parseInt()
int num1 = Integer.parseInt(String.valueOf(a));
int num2 = Integer.parseInt(String.valueOf(b));
System.out.println(num1); // 5
System.out.println(num2); // 9
Tokens
Tokens
• testvariable
• a
• i
• Test_Variable
• _testvariable
• $testvariable
• sum_of_array
• TESTVARIABLE
• jtp123
Invalid identifiers
• Have a constant value and a fixed data type and are often known as
constants in Java.
• Literals are assigned to a variable to provide a value to the variable.
• Example: int cost =340 ;
Types of Literals in Java
Integer Literals
1. binary (base 2)
2. decimal (base 10)
3. octal (base 8)
4. hexadecimal (base 16)
Decimal Integer:
• int octal_int=077;
Hexadecimal Integer:
• int hexadec_int=0x1ff2;
Binary Integer
• int binary_int=0b1010101;
Example
int decimal_int=1234;
int octal_int=077;
int hexadec_int=0x1ff2;
int binary_int=0b1010101;
System.out.println("This is a Decimal Literal: "+decimal_int);
System.out.println("This is an Octal Literal: "+octal_int);
System.out.println("This is a Hexa Decimal Literal: "+hexadec_int);
System.out.println("This is a Binary Literal: "+binary_int);
Output
• 123.45 //Legal
• 122.32E5 //Legal
• 231.12F //Legal
• 1/4 // Illegal Symbol Used “/”
• 1.7.5 //Illegal, as two decimal points used.
• 1,234.56 // Illegal, as commas are not allowed
• 123.E4 //Illegal, as E cannot precede the point.
• 321E //Illegal, as the exponent part is incomplete.
Example
float val_float=1.7732f;
double val_double=1.7732d;
float val_exponent=123E4f;
boolean flag1=true;
boolean flag2=false;
String company=“Atria”;
String null_Literal=null;
System.out.println("This is a String Literal: "+company);
System.out.println("This is a null Literal: "+null_Literal);
Output:
This is a String Literal: Atria
This is a null Literal: null
Character Literal
• we can specify char literal as integral literal, which represents the Unicode
value of the character, and that integral literal can be specified either in
• char ch = 062;
Unicode Representation:
char ch = 'a';
char b = 0789;
// Unicode representation
char c = '\u0061';
System.out.println(ch);
System.out.println(b);
System.out.println(c);
Output
• a
• error:Integer number too large
• a
OPERATORS IN JAVA
Operators in java
Assignment assignment = += -= *= /= %=
Java Unary Operator
class OperatorExample{
public static void main(String args[]){
int x=10;
System.out.println(x++); 10
System.out.println(++x); 12
12
System.out.println(x--);
System.out.println(--x); 10
}}
SHIFT OPERATOR
Shift Operator
• Left shift operator shifts all bits towards the left by a certain number of
specified bits.
• It is denoted by <<.
Java Left Shift Operator Example
class OperatorExample{
public static void main(String args[]){
System.out.println(10<<2); //10*2^2=10*4=40
System.out.println(10<<3); //10*2^3=10*8=80
System.out.println(20<<2); //20*2^2=20*4=80
System.out.println(15<<4); //15*2^4=15*16=240
}}
Right Shift Operator
• Right shift operator shifts all bits towards the right by a certain number of
specified bits.
• It is denoted by >>.
• When we shift any number to the right, the least significant bits (rightmost) are
discarded and the most significant position (leftmost) is filled with the sign bit.
Java Right Shift Operator Example
class OperatorExample{
public static void main(String args[]){
System.out.println(10>>2); //10/2^2=10/4=2
System.out.println(20>>2); //20/2^2=20/4=5
System.out.println(20>>3); //20/2^3=20/8=2
}}
Relation operators
Relation operators
> Check if operand on the left is greater than operand on the right
int a, b;
a=40;
b=30;
System.out.println("a == b = " + (a == b) );
System.out.println("a != b = " + (a != b) );
System.out.println("a > b = " + (a > b) );
System.out.println("a < b = " + (a < b) );
System.out.println("b >= a = " + (b >= a) );
System.out.println("b <= a = " + (b <= a) );
Bitwise operators
Bitwise operators
class Main {
public static void main(String[] args) {
class Main {
public static void main(String[] args) {
class Main {
public static void main(String[] args) {
For example,
• Consider an integer 35.
• As per the rule, the bitwise complement of 35 should be -(35 + 1) = -36.
Program
class Main {
public static void main(String[] args) {
// bitwise complement of 35
result = ~number;
System.out.println(result); // prints -36
}
}
Logical Operators
Logical operators
// && operator
System.out.println((5 > 3) && (8 > 5)); // true
System.out.println((5 > 3) && (8 < 5)); // false
// || operator
System.out.println((5 < 3) || (8 > 5)); // true
System.out.println((5 > 3) || (8 < 5)); // true
System.out.println((5 < 3) || (8 < 5)); // false
// ! operator
System.out.println(!(5 == 3)); // true
System.out.println(!(5 > 3)); // false
}
}
Java Ternary Operator
System.out.println(result);