Introduction To Java
Introduction To Java
What is JAVA
Java is a popular programming language, created in 1995.
It is used for:
• Mobile applications (specially Android apps)
• Desktop applications
• Web applications
• Web servers and application servers
• Games
• Database connection
Why Use Java?
• Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
• It is one of the most popular programming languages in the world
• It has a large demand in the current job market
• It is easy to learn and simple to use
• It is open-source and free
• It is secure, fast and powerful
• It has huge community support (tens of millions of developers)
• Java is an object oriented language which gives a clear structure to programs and
allows code to be reused, lowering development costs
• As Java is close to C++ and C#, it makes it easy for programmers to switch to Java
or vice versa
Documentation section
• It includes basic information about a Java program. The information includes
the author's name, date of creation, version, program name, company
name, and description of the program.
• Whatever we write in the documentation section, the Java compiler ignores the statements
during the execution of the program.
• To write the statements in the documentation section, we use comments. The comments
may be single-line, multi-line, and documentation comments.
• Single-line Comment: It starts with a pair of forwarding slash (//). For example:
//First Java Program
Multi-line Comment: It starts with a /* and ends with */. We write between these two
symbols. For example:/*It is an example of multiline comment*/
Documentation Comment: It starts with the delimiter (/**) and ends with */. For example:
/**It is an example of documentation comment*/
Package Declaration
• , the variables and constants are defined just after the class definition.
The variables and constants store values of the parameters. It is used
during the execution of the program.
class Student //class definition
{
String sname; //variable
int id;
double percentage;
}
Main Method Class
• It is essential for all Java programs. Because the execution of all Java programs
starts from the main() method. In other words, it is an entry point of the class. It
must be inside the class. Inside the main method, we create objects and call the
methods. We use the following statement to define the main() method:
}
• When the main method is declared public, it means that it can also be
used by code outside of its class, due to which the main method is
declared public.
• The word static used when we want to access a method without
creating its object, as we call the main method, before creating any
class objects.
• The word void indicates that a method does not return a value. main()
is declared as void because it does not return a value.
• main is a method; this is a starting point of a Java program.
• String[] args- It is an array where each element of it is a string,
which has been named as "args". If your Java program is run through the
console, you can pass the input parameter, and main() method takes it as
input.
• The methods are the set of instructions that we want to perform. These instructions
execute at runtime and perform the specified task. For example:
public class Demo //class definition
{
public static void main(String args[])
{
void display()
{
System.out.println("Welcome to javatpoint");
}
//statements
}
}
Simple
Object-Oriented
Portable
Platform independent
Secured
Robust
Architecture neutral
Interpreted
High Performance
Multithreaded
Distributed
Features of Java
• Simple: Java is a simple language because its syntax is simple, clean, and easy to understand.
Complex and ambiguous concepts of C++ are either eliminated or re-implemented in Java.
• Object-Oriented: In Java, everything is in the form of the object. It means it has some data
and behavior. A program must have at least one class and object.
• Robust: Java makes an effort to check error at run time and compile time.
• Secure: Java is a secure programming language because it has no explicit pointer and
programs runs in the virtual machine. Java contains a security manager that defines the access
of Java classes.
Features of Java
• Platform-Independent: Java provides a guarantee that code writes once and run anywhere. This
byte code is platform-independent and can be run on any machine.
Java Variables
• A variable is a location in memory (storage area) to hold
data.
• To indicate the storage area, each variable should be
given a unique name (identifier).
Create Variables in Java
int speedLimit = 80;
Here, speedLimit is a variable of int data type and we
have assigned value 80 to it.
Change values of variables
• Java is case sensitive. Hence, age and AGE are two different variables.
int age = 24;int AGE = 25;
• Data types specify the type of data that can be stored inside variables in Java.
1.Primitive data types: The primitive data types include boolean, char, byte, short,
int, long, float and double.
• The boolean data type has two possible values, either true or false.
• The byte data type can have values from -128 to 127 (8-bit signed two's complement integer).
• If it's certain that the value of a variable will be within -128 to 127, then it is used instead of int to
save memory.
• Default value: 0
class Main {
public static void main(String[] args) {
byte range;
range = 124;
System.out.println(range); // prints 124
}
}
short type
• The short data type in Java can have values from -32768 to 32767 (16-bit signed two's
complement integer).
• If it's certain that the value of a variable will be within -32768 and 32767, then it is used
instead of other integer data types (int, long).
• Default value: 0
short temperature;
temperature = -200;
System.out.println(temperature); // prints -200
}
}
Java Operators
1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Unary Operators
6. Bitwise Operators
Java Arithmetic Operators
// declare variables
int a = 12, b = 5;
// addition operator
System.out.println("a + b = " + (a + b));
// subtraction operator
System.out.println("a - b = " + (a - b));
// multiplication operator
System.out.println("a * b = " + (a * b));
// division operator
System.out.println("a / b = " + (a / b));
// modulo operator
System.out.println("a % b = " + (a % b));
}
}
Java Assignment Operators
// create variables
int a = 4;
int var;
// <= operator
System.out.println(a <= b); // true
}
}
Java 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 Unary Operators
• Unary operators are used with only one operand. For example, ++ is a
unary operator that increases the value of a variable by 1. That is, ++5 will
return 6.
• Different types of unary operators are:
class Main {
public static void main(String[] args) {
// declare variables
int a = 12, b = 12;
int result1, result2;
// original value
System.out.println("Value of a: " + a);
// increment operator
result1 = ++a;
System.out.println("After increment: " + result1);
// decrement operator
result2 = --b;
System.out.println("After decrement: " + result2);
}
Constructors in Java
• At the time of calling constructor, memory for the object is allocated in the
memory.
• Every time an object is created using the new() keyword, at least one
constructor is called.
Rules for creating Java constructor
• Parameterized constructor
Java Default Constructor
1.<class_name>(){}
/Java Program to create and call a default constructor
you are not creating any constructor so compiler provides
you a default constructor.
class Bike1{
//creating a default constructor
Bike1(){System.out.println("Bike is created");}
//main method
public static void main(String args[]){
//calling a default constructor
Bike1 b=new Bike1();
}
}
o/p.. Bike is created
/Let us see another example of default constructor
//which displays the default values
class Student3{
int id;
String name;
//method to display the value of id and name
void display(){System.out.println(id+" "+name);}
Output:
0 null
0 null
Java Parameterized Constructor
constructor.