Java
Java
What is Java?
Java is a popular high-level,object-oriented programming language that was originally developed by
Sun Microsystems and released in 1995. Currently, Java is owned by Oracle, and more than 3 billion
devices run Java. Java runs on a variety of pla orms, such as Windows, Mac OS, and the various
versions ofUNIX. Today Java is being used to develop numerous types of so ware applica ons,
including desktop apps, mobile apps, web apps, games, and much more.
Java programming language was originally developed by Sun Microsystems, which was ini ated by
James Gosling and released in 1995 as a core component of Sun Microsystems' Java pla orm (Java 1.0
[J2SE]).
Java Features
Java is a feature-rich language. Java is evolving con nuously with every update, and updates are
coming every six months. Following are some of the main features of the Java language:
Object Oriented: Java is a pure object-oriented language, and everything in Java is an object. Java
supports OOPS principles like Inheritance, Encapsula on, Polymorphism, Classes , and so on. Java itself
can be extended as well, being based on an object model.
Pla orm Independent: Java code is pla orm independent. A Java code is not compiled into machine-
specific code; it is compiled into a pla orm-neutral byte code. This byte code is executed by JVM which
runs the code on the underlying pla orm. This capability makes Java a Write Once Run Anywhere
language.
Easy To Learn: Java inherits features from C and C++, and developers can easily learn Java if they know
any of the C or C++ languages. Even for someone new to computer languages, Java is very easy to learn
from scratch.
Secure: Java is secure by architecture. A developer is not required to directly interact with the
underlying memory or opera ng system. Java provides automa c garbage collec on, so developers
are not required to worry about memory leaks, management, etc.
Architectural-Neutral: Java byte code can be executed on any kind of processor. JRE automa cally
handles the code execu on on different types of processors.
Portable: A Java code wri en on a Windows machine can be executed without any code change on
MacOS and vice versa. There is no need to make any opera ng system-specific code changes.
Robust: Java is a very robust language with very strong compile- me error checks, strict type checking,
and run me excep on handling.
Mul threading: Java provides inbuilt support for mul processing and mul threading. Java provides
thread handling, monitors, deadlock handling, racing condi ons, etc.
High Performance: Java, although being interpreted, is s ll very performant. The JIT(Just In Time)
compilerhelps in improving performance.
Distributed: Java is designed for distributed systems and is the most popular language for developing
internet-based applica ons as the internet is a distributed environment.
Page 1 of 12
Unit - I
(Java Virtual Machine) is a specifica on and can have different implementa ons, as long as they adhere
to the specs.
What is JDK?
JDK is an abbrevia on for Java Development Kit which includes all the tools, executables, and binaries
required to compile, debug, and execute a Java Program.JDK is pla orm dependent i.e. there are
separate installers for Windows, Mac, and Unix systems. JDK includes both JVM and JRE and is en rely
responsible for code execu on.
What is JRE?
JRE is a Java Run me Environment which is the implementa on of JVM i.e. the specifica ons that are
defined in JVM are implemented and create a corresponding environment for the execu on of code.
JRE comprises mainly Java binaries and other classes to execute the program like JVM which physically
exists. Along with Java binaries JRE also consists of various technologies of deployment, user interfaces
to interact with code executed, some base libraries for different func onali es, and language
What is JVM?
JVM is the abbrevia on for Java Virtual Machine which is a specifica on that provides a run me
environment in which Java byte code can be executed i.e. it is something that is abstract and its
implementa on is independent of choosing the algorithm and has been provided by Sun and other
companies. It is JVM which is responsible for conver ng Byte code to machine-specific code. It can also
run those programs which are wri en in other languages and compiled to Java bytecode.
Sr.
Key JDK JRE JVM
No.
Page 2 of 12
Unit - I
Sr.
Key JDK JRE JVM
No.
Page 3 of 12
Unit - I
2. Comment Sec on
/* This is my first java program.
* This will print 'Hello World' as the output
*/
These lines being in /* */ block are not considered by Java compiler and are comments. A comment
helps to understand program in a be er way and makes code readable and understandable.
4. Keywords Used
Let's check the purpose of each keyword in this line.
public − defines the scope of the main method. Being public, this method can be called by
external program like JVM.
sta c − defines the state of the main method. Being sta c, this method can be called by
external program like JVM without first crea ng the object of the class.
void − defines the return type of the main method. Being void, this method is not returning
any value.
main − name of the method
String []args − arguments passed on command line while execu ng the java command.
5. System.out.println() Method
System.out.println("Hello World"); // prints Hello World
System.out represents the primary console and its println() method is taking "Hello World" as input
and it prints the same to the console output.
Java Comments
Java comments are text notes wri en in the code to provide an explana on about the source code.
The comments can be used to explain the logic or for documenta on purposes.
Documenta on Comment
The documenta on comments are used for wri ng the documenta on of the source code. The
documenta on comments start with a forward slash followed by the two asterisks (/**), end with an
asterisk followed by a backward slash (*/), and all lines between the start and end must start with an
asterisk (*).
Page 4 of 12
Unit - I
Page 5 of 12
Unit - I
Example
boolean one = true;
Page 6 of 12
Unit - I
byte byteValue1 = 2;
byte byteValue2 = 4;
byte byteResult = (byte)(byteValue1 + byteValue2);
short shortValue1 = 2;
short shortValue2 = 4;
short shortResult = (short)(shortValue1 + shortValue2);
int intValue1 = 2;
int intValue2 = 4;
int intResult = intValue1 + intValue2;
Output
Byte: 6
Short: 6
Int: 6
Long: 6
Float: 6.0
Double: 6.0
Page 7 of 12
Unit - I
Boolean: true
Char: A
The default value of any reference variable is null. A reference variable can be used to refer to any
object of the declared type or any compa ble type.
The following example demonstrates the reference (or, object) data types.
Animal animal = new Animal("giraffe");
Syntax
Below is the syntax for narrowing type cas ng i.e., to manually type conversion:
double doubleNum = (double) num;
The above code statement will convert the variable to double type.
Page 8 of 12
Unit - I
Fist you need to import the Scanner class to use its methods. To import the Scanner class, use the
following import statement −
import java.u l.Scanner
The above statement will wait for an integer input from the user. When user provides an integer value,
that will be assign to "age" variable.
Java Operators
There are different types of operators in Java, we have listed them below −
Arithme c Operators
Assignment Operators
Rela onal Operators
Logical Operators
Bitwise Operators
Misc Operators
+ (Addi on) Adds values on either side of the operator. A + B will give 30
Page 9 of 12
Unit - I
Simple assignment operator. Assigns values from C = A + B will assign value of A +
=
right side operands to le side operand. B into C
Page 10 of 12
Unit - I
Page 11 of 12
Unit - I
Binary Ones Complement Operator (⁓A ) will give -61 which is 1100 0011 in 2's
⁓ (bitwise
is unary and has the effect of complement form due to a signed binary
compliment)
'flipping' bits. number.
Instanceof Operator
This operator is used only for object reference variables. The operator checks whether the object is of
a par cular type (class type or interface type). instanceof operator is wri en as −
( Object reference variable ) instanceof (class/interface type)
Page 12 of 12