Java Notes
Java Notes
JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle
Corporation. It was developed by James Gosling and Patrick Naughton. It is a simple
programming language. Writing, compiling and debugging a program is easy in java. It
helps to create modular programs and reusable code.
Token: Java tokens are smallest elements of a program which are identified
by the compiler. Tokens in java include identifiers, keywords, literals, operators
and, separators.
Keywords: Java keywords are also known as reserved words. Keywords are particular words
which act as a key to a code. These are predefined words by Java so it cannot be used as a
variable or object name.
Keywords are predefined; reserved words used in Java programming that have special meanings
to the compiler. For example:
int score;
Here, int is a keyword. It indicates that the variable score is of integer type (32-bit signed two's
complement integer).
You cannot use keywords like int, for, class, etc as variable name (or identifiers) as they are part
of the Java programming language syntax. Here's the complete list of all keywords in Java
programming.
Here are some valid identifiers:
score
level
highestScore
number1
convertToString
JAVA Constant:
A constant is an entity in programming that is immutable. In other words, the value that cannot
be changed. In Java, to declare any variable as constant, we use static and final modifiers. It is
also known as non-access modifiers. According to the Java naming convention the identifier
name must be in capital letters.
Static and Final Modifiers
The purpose to use the static modifier is to manage the memory.
It also allows the variable to be available without loading any instance of the class in which it is
defined.
The final modifier represents that the value of the variable cannot be changed. It also makes the
primitive data type immutable or unchangeable.
The syntax to declare a constant is as follows:
Java is a platform-independent programming language. It means that we can run Java on the
platforms that have a Java interpreter. It is the reason that makes the Java platform-
independent. The Java interpreter converts the Java byte code (.class file) into the code
understand by the operating system.
If the JVM(Java Virtual Machine) is installed on any system it means that the platform is JVM
enabled. The platform performs all the tasks of the Java run-time system. It loads the Java class
file and interprets the compiled byte-code.
As the Java compiler compiles the source code into the Java byte code. In the same way, the Java
interpreter converts or translates the byte code into the machine-understandable format i.e.
machine code, after that the machine code interacts with the operating system.
The Scanner class of the java.util package is used to read input data from different sources like
input streams, users, files, etc. Java Scanner Methods are used for taking inputs.
The Scanner class provides various methods that allow us to read inputs of different types.
Method Description
Simple Programs:
1. Sum of two numbers:
public class AddTwoNumbers
{
public static void main(String[] args)
{
int num1 = 5, num2 = 15, sum;
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
}
}
2. Sum of two numbers using Scanner
import java.util.Scanner;
public class AddTwoNumbers2
{
public static void main(String[] args)
{
int num1, num2, sum;
Scanner sc = new Scanner(System.in);
System.out.println("Enter First Number: ");
num1 = sc.nextInt();
System.out.println("Enter Second Number: ");
num2 = sc.nextInt();
sc.close();
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
}
}
3. Program to calculate the sum of natural numbers using for loop
import java.util.Scanner;
class AreaTriangleDemo
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the width of the Triangle:");
double base = scanner.nextDouble();
System.out.println("Enter the height of the Triangle:");
double height = scanner.nextDouble();
//Area = (width*height)/2
double area = (base* height)/2;
System.out.println("Area of Triangle is: " + area);
}
}