Our topic is What is java?
Under this I am going to explain following things .
- History of Java
- Features of Java
- JDK ,JRE and JVM
- Simple Java Program
- Java variables
- Java Data Type
- Java Operators
Before moving to history of java It is better t know about the history of programming languages.
What is programming?
It is series of instructions to a computer to accomplish a task.Programming languages are used to write programs.
What is a programming language?
Set of commands for programming computers.
Programming language generations
1st generation
- Machine level programming languages
- Fast and efficient,executed directly on the cpu
- Difficult for humans to read,write and debug
2nd generation
Assembly languages
- Simple instructions
- Assembler translates into machine code
3rd generation
High level,general purpose
- Easier for humans to read,write.debug
- compiler translates into machine code before running
- Interpreter translates into machine code at run time
4th generation
Specification languages,query languages, report generators,system engineering
5th generation
Solve problems using rules/constraints rather than algorithms used in Artificial Intelligence
Programming language Paradigms
1.Procedural :procedures,sequential execution of code are basic building blocks of program
ex: FORTRAN,Pascal,C
2. Object-Oriented :Program is designed around the objects required to solve the problem.
ex: Simula ,Smalltalk ,C++,Java,C#
3.Functional :Program is designed around the evaluation of functions
ex: Haskell
4.Logic : Program is declarative , based on mathematical logic
ex: Prolog5.Scripting : used for text processing
ex: PHP,Python,Ruby
Steps of building a program
1.Developing the algorithm
2. Writing the program
3.Documenting the program
4.Testing and debugging the program
Alpha testing - Testing within the organization
Beta testing - Testing by sophisticated users from outside the organization.
Object Oriented Languages(according to Alan kay)
Everything is an object. A program is a bunch of objects telling each other what to do, by sending
messages. each object has its own memory.Every object has a type(class). All objects of the same type can receive the same message.
1)What is Java?
Java is an object -oriented programming language. But it is not a pure OOP language.
Also it is a platform.(java run time environment and API)
James Gosling has introduced this through Green Project.
Usage of Java
- Desktop Applications (ex: media player,antivirus)
- Web Applications (ex: banking applications)
- Mobile
- Smart Card
- Games
Types of Java Application
1. Standalone Application (desktop/window-based applications)
2. Web applications (servlet,JSP,spring)
3.Enterprise Application(ex: banking application, in jave use EJB for this)
4.Mobile Application (Android and Java ME)
2) Characteristics Of Java
- Simple (based on c++ but very simpler than c++)
- Object oriented
- Distributed (allow network functionalities)
- Robust (compile time and run time error handling)
- Secure
- Architecture -Natural (write once run anywhere) with JVM you can write one program that will run on any platform .
- Portable (programs can run on any platform without being recompiled)
3) JDK
Java Development kit - tools needed to develop java programs
Java run time environment - to run programs
The target of the JRE is execution of java files/ Java development
JRE = JVM+Java package class (lang,math,util) +run time libraries
JRE contains JVM class libraries and other supporting files. Doesn't contain any development tools(compiler,debugger etc)
Actually JRE is a set of software tools which are used for developing java apps used to provide run time environment. It is the implementation of the JVM. It physically exist. Contains set of libraries and other files that JVM uses at run time.
JDk =Tools+JRE
Tools = compiler (javac) + application launcher(java) +applet+viewer
Here application launcher opens JRE , load class and main method
Java Code ---------->compiler -----------> Byte code
JDK is software development environment which is used to develop java apps and applets.
JVM
JVM runs programs . It uses classes , libraries and other supporting files provided by JRE. IT is designed by sun micro systems.
Byte code ---------->JVM ------------>machine code
Also JVM called virtual (abstract machine - doesn't physically exist)........ It means write once run anywhere .
It provides machine interface which doesn't depend on platform (os, machine.hardware)
JVM ,
- loads code
- verifies code
- execute code
- provide run time environment
Basic parts of Java Virtual Machine
- A set of registers
- A stack
- A garbage collected heap
- An execution environment
- An instruction set
- A constant pool
- A method storage area
MyProgram.java
-----------------------------------------
API
-----------------------------------------
JVM
----------------------------------------
Hardware -Based platform
API -large collection of ready -made software components that provide many useful capabilities.
Source code ........>compiler ........> byte code......> interpreter ..........>screen
What is java byte code?
It is the result of the compilation of a java program , an intermediate representation of that program
which is machine independent. Ii gets processed by the JVM.
5 ) Simple Java Program
package today1;
public class Today1 {
public static void main(String[] args) {
System.out.println("Hello Uresha");
}
}
It is the result of the compilation of a java program , an intermediate representation of that program
which is machine independent. Ii gets processed by the JVM.
5 ) Simple Java Program
- One public class per source code file
- Statement should terminated by semi colon
- File name should similar to public class name
- File may have many non public classes
- Every java application should have main method and it starts the program execution
- Class name should be start with capital and can use CamelCase for further purpose
- Methods/variables should start with simple ( ex: methodName)
- Constants shod be capital ( ex : CONSTANT_MAX)
- Java is case sensitive.
- There are reserved words in java. You can't use those words(keywords) as a class name,variable or method in java
package today1;
public class Today1 {
public static void main(String[] args) {
System.out.println("Hello Uresha");
}
}
output : Hello Uresha
- class keyword is used to declare a class in java
- public keyword is an access modifier which represents visibility. (visible to all)
- static is a keyword.(no need to create an object to invoke the static method)
- void is return type of the method. It means it doesn't return any value.
- main represents the starting point of the program
- String[]args is used for command line argument.
- System.out.println() is used to print statement.
Difference between System.out.print() and System.out.println()
public class Test{
public static void main(String[] args){
System.out.println("Hello friends"); // This prints next line
System.out.print ("My name is "); // This prints in the same line
System.out.print("uresha");
}
}
output
Hello friends
My name is uresha
5) Java Variables
A variable is a name of memory location.(in RAM)
It should be declared before it used.
It has name and data type.
There are 3 types of variables in java. (local , instance and static )
Local Variables - declare inside a method . visible only inside of the method.
Instance Variable - any variable which is defined in class body and outside method's body and doesn't have static modifier.
Static Variable (class variable) - use static modifier. Only copy of the variables shared with all instance of class
There are two stages in a variable.
1. Declare the variable.
ex: int x (here int is data type and x is variable name)
2.Initialize the variable (assigning value for it)
ex: int x= 10
Types of variable in java
ex:
public class A {
int i =45; // instance variable
static int n = 100; // static variable
void method (){
int a = 60; //local variable
}
}
Write a java program to add two integers and get summation of those two numbers.
public class Sum{
public static void main(String[]args){
int x = 10;
int y =20;
System.out.println("The summation of two integers is :" +( x+ y));
}
}
int i =45; // instance variable
static int n = 100; // static variable
void method (){
int a = 60; //local variable
}
}
Write a java program to add two integers and get summation of those two numbers.
public class Sum{
public static void main(String[]args){
int x = 10;
int y =20;
System.out.println("The summation of two integers is :" +( x+ y));
}
}
6 )Data Types in java
Mainly there are two data types in java .
According to above image there are 8 primitive data types in java. Out of the integer and floating point are numeric and others are non numeric.
Specially "String " is not primitive in java.
Also you can see data type and it's size from bits as follows.
- byte - 8
- short - 16
- int -32
- long -64
- float-32
- double -64
- char - 16
- boolean -1
Data conversion (assignment/promotion/type casting)
Widening Conversion(narrow to border /implicit casting )
In here value is converted from lower size data type to Upper size data type without loss of information.
ex:
int a = 100;
double b = a;
System.out.println(b);
Narrowing Conversion (explicit cast)
In here value is converted from Upper size data type to Lower size data type with loss of information.
ex:
double a = 100;
int b = (int ) a;
System.out.println(b);
My next question is "Is Java a pure object oriented language"
No.
Java is not pure object oriented language . Because it supports primitive data types such as int, byte, long etc. to be used which are not objects. When we consider about pure object oriented language like smalltalk , there are no primitive types. Boolean , int and methods are all objects.
7. Java Operators
When we consider about the usage of Unary operators as follows.
*incrementing / decrementing a value by one
int x =12;
System.out.println(x ); //12
System.out.println(x ++); // 12 (post - increment )
System.out.println(x ); // 13
System.out.println( ++ x ); //14 (pre -increment)
System.out.println( -- x ); // 13 (pre - decrement)
System.out.println(x ); // 13
System.out.println(x --); // 13 (post -decrement)
System.out.println(x ); //12
*boolean values
boolean a =true ;
boolean b = false;
System.out.println(a); // true
System.out.println(b); //false
System.out.println(!a); // false
System.out.println(!b); // true
Arithmetic Operators
int a = 10;
int b = 5 ;
System.out.println(a + b); // 15 - addition
System.out.println(a - b); // 5 - subtraction
System.out.println(a / b); // 2 - division
System.out.println(a * b); // 50 - multiplication
System.out.println(a % b); // 0 - modulus
Left Shift Operator
System.out.println(20<<2); //20 *2^2 =20*4 = 80
System.out.println(10<<3); // 10 * 2^3 =10*8 =80
Right Shift Operator
System.out.println(20>>2); //20/2^2 = 20/4 =5
System.out.println(80>>3); // 80/2^3 = 80/8 =10
>> vs >>>
For positive numbers it works as same
System.out.println(20>>2 ); // 5
System.out.println(20>>>2 ); // 5
For negative number it works as different.Try with this
System.out.println(-20>>2 ); // -5
System.out.println(-20>>>2 ); // ?
Bitwise Operators
a b AND(a & b) OR(a | b) XOR(a^ b)
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
A 0011 1100
B 0000 1101
A& B 0000 1100
A | B 0011 1101
A^B 0011 0001
Logical Operators
&& - only check second condition if the first condition is true.
|| - only check second if first is not true,
int a =5 ,b = 8;
if(a>3 && b>4) {
System.out.println(" both conditions are correct");
}
if (a>10 && b>2){
System.out.println("only second one correct");
}
if (a>2 || b>5){
System.out.println("both conditions are correct");
}
if (a>2 || b>10){
System.out.println("only first one is correct");
}
output
run:
both conditions are correct
both conditions are correct
only first one is correct
BUILD SUCCESSFUL (total time: 0 seconds)
7. Java Operators
- Unary -take one argument
- Binary - take two arguments
- Ternary - take three argument
When we consider about the usage of Unary operators as follows.
*incrementing / decrementing a value by one
int x =12;
System.out.println(x ); //12
System.out.println(x ++); // 12 (post - increment )
System.out.println(x ); // 13
System.out.println( ++ x ); //14 (pre -increment)
System.out.println( -- x ); // 13 (pre - decrement)
System.out.println(x ); // 13
System.out.println(x --); // 13 (post -decrement)
System.out.println(x ); //12
*boolean values
boolean a =true ;
boolean b = false;
System.out.println(a); // true
System.out.println(b); //false
System.out.println(!a); // false
System.out.println(!b); // true
Arithmetic Operators
int a = 10;
int b = 5 ;
System.out.println(a + b); // 15 - addition
System.out.println(a - b); // 5 - subtraction
System.out.println(a / b); // 2 - division
System.out.println(a * b); // 50 - multiplication
System.out.println(a % b); // 0 - modulus
Left Shift Operator
System.out.println(20<<2); //20 *2^2 =20*4 = 80
System.out.println(10<<3); // 10 * 2^3 =10*8 =80
Right Shift Operator
System.out.println(20>>2); //20/2^2 = 20/4 =5
System.out.println(80>>3); // 80/2^3 = 80/8 =10
>> vs >>>
For positive numbers it works as same
System.out.println(20>>2 ); // 5
System.out.println(20>>>2 ); // 5
For negative number it works as different.Try with this
System.out.println(-20>>2 ); // -5
System.out.println(-20>>>2 ); // ?
Bitwise Operators
a b AND(a & b) OR(a | b) XOR(a^ b)
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
A 0011 1100
B 0000 1101
A& B 0000 1100
A | B 0011 1101
A^B 0011 0001
Logical Operators
&& - only check second condition if the first condition is true.
|| - only check second if first is not true,
int a =5 ,b = 8;
if(a>3 && b>4) {
System.out.println(" both conditions are correct");
}
if (a>10 && b>2){
System.out.println("only second one correct");
}
if (a>2 || b>5){
System.out.println("both conditions are correct");
}
if (a>2 || b>10){
System.out.println("only first one is correct");
}
output
run:
both conditions are correct
both conditions are correct
only first one is correct
BUILD SUCCESSFUL (total time: 0 seconds)
Difference between bitwise and logical operators (short -circuit )
From name you can guess bitwise operators at bit level and perform AND logical operation to each bit, while logical operators operate an boolean variables only.
Main difference is there short circuit behavior , which means if there are two or more conditions , which are joined using && , || operators then not all conditions are tested.
Ternary Operator
int a =10;
int b= 3;
int max= (a>b)?a:b;
System.out.println(max);
In here firstly check the condition(a>b) , then if it is true print a otherwise b.
I hope to meet you with very essential part of java from next topic. Thank you guys !








No comments:
Post a Comment