0% found this document useful (0 votes)
93 views9 pages

Java Notes

This document provides an introduction and overview of the Java programming language. It discusses that Java was created by Sun Microsystems in 1991 and is now owned by Oracle. It notes that Java is an easy to use language for writing, compiling, and debugging programs and helps create modular and reusable code. It then discusses the Java Virtual Machine (JVM) and how it executes Java bytecode. The rest of the document provides explanations and examples of key Java concepts like classes, objects, variables and data types, operators, control flow, methods, arrays, and more. It also provides a simple example Java program and discusses how to compile and run a Java program.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
93 views9 pages

Java Notes

This document provides an introduction and overview of the Java programming language. It discusses that Java was created by Sun Microsystems in 1991 and is now owned by Oracle. It notes that Java is an easy to use language for writing, compiling, and debugging programs and helps create modular and reusable code. It then discusses the Java Virtual Machine (JVM) and how it executes Java bytecode. The rest of the document provides explanations and examples of key Java concepts like classes, objects, variables and data types, operators, control flow, methods, arrays, and more. It also provides a simple example Java program and discusses how to compile and run a Java program.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

INTERDUCTION

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.

Java Virtual Machine (JVM)


This is generally referred as JVM. Before, we discuss about JVM lets see the phases of program
execution. Phases are as follows: we write the program, then we compile the program and at last
we run the program.
1) Writing of the program is of course done by java programmer like you and me.
2) Compilation of program is done by javac compiler, javac is the primary java compiler
included in java development kit (JDK). It takes java program as input and generates java
bytecode as output.
3) In third phase, JVM executes the bytecode generated by compiler. This is called program run
phase.
To start JAVA program in your system:
Download jdk1.4 or higher version and install it.
Download bluej from bluej.org(compatible for your system), then install it.
BlueJ icon will be created in your Desktop.
Click on project and then new project(for the first time).
Click on new class & write the class name.
Open class remove the content & type your program.
Compile & close it.
Give a right click on mouse and click on void main to get result.
Step by step guide:

1. Click on this icon (BlueJ) on the active desktop

2. Click on open new project


3. Click on new class.

4. Now specify the class name(eg:Hello)

5. Give a right click on it and select open editor.

6. Select all default write-up and remove it.


7. Now write your program as shown then
click on compile, if error then rectify
otherwise close it.

8. Now give a right click on the class that


you have created and select void main as
shown.

Simple Java Program:


public class FirstJavaProgram {
public static void main(String[] args)
{
System.out.println("This is my first program in java");
}//End of main
}//End of FirstJavaProgram Class
Every program must be written in a class. Class name should be the same name in which file is
saved. A class can contain a number of paragraph/module with a name( at least main paragraph
must same as C that you have done earlier). For printing the result, you can use following
statement:
System.out.println(“statements to be printed”); : It print the result and go to next line.
System.out.print(“statements to be printed”); : It print the result and remain in the same line.
// for single line documentation (a non executable statement for programmer reference)

/* Statements */ : multi line documentation.

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).

Ideantifiers: Identifiers in Java are symbolic names used for identification.


They can be a class name, variable name, method name, package name, constant name, and
more. However, In Java, There are some reserved words that cannot be used as an identifier.

Rules for Naming an Identifier:


1. Identifiers cannot be a keyword.
2. Identifiers are case-sensitive.
3. It can have a sequence of letters and digits. However, it must begin with a letter, $ or _. The first
letter of an identifier cannot be a digit.
4. It's a convention to start an identifier with a letter rather and $ or _.
5. Whitespaces are not allowed.
6. Similarly, you cannot use symbols such as @, #, and so on.

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

Here are some invalid identifiers:


class
float
1number
highest Score
@pple

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:

static final datatype identifier_name=value;


For example, price is a variable that we want to make constant.

static final double PRICE=432.78;


Where static and final are the non-access modifiers. The double is the data type and PRICE is the
identifier name in which the value 432.78 is assigned.

Variables & Types:


A variable provides us with named storage that our programs can manipulate. Each variable in
Java has a specific type, which determines the size and layout of the variable's memory; the
range of values that can be stored within that memory; and the set of operations that can be
applied to the variable.
Basic form of a variable declaration −

data type variable [ = value][, variable [ = value] ...] ;


Example
int a, b, c; // Declares three ints, a, b, and c.
int a = 10, b = 10; // Example of initialization
byte B = 22; // initializes a byte type variable B.
double pi = 3.14159; // declares and assigns a value of PI.
char a = 'a'; // the char variable a iis initialized with value 'a'
Although Java is object oriented, not all types are objects. It is built on top of basic variable types
called primitives.

Here is a list of all primitives in Java:

byte (number, 1 byte)


short (number, 2 bytes)
int (number, 4 bytes)
long (number, 8 bytes)
float (float number, 4 bytes)
double (float number, 8 bytes)
char (a character, 2 bytes)
boolean (true or false, 1 byte)
Java is a strong typed language, which means variables need to be defined before we use them.

Some Examples showing variable declaration:


int myNumber; myNumber = 5;
double d = 4.5;
d = 3.0;
float f = (float) 4.5;
char c = 'g';
int num = 5; String s = "I have " + num + " cookies"; //Be sure not to use "" with primitives.
boolean b = false;

Programs & its compilation:


When we compile Java program (javac: JAVA Compiler) java compiler converts the source code
into byte code.

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.

Parameters used in Java Program


Let's see what is the meaning of class, public, static, void, main, String[], System.out.println().

class keyword is used to declare a class in java.


public keyword is an access modifier which represents visibility. It means it is visible to all.
static is a keyword. If we declare any method as static, it is known as the static method. The core
advantage of the static method is that there is no need to create an object to invoke the static
method. The main method is executed by the JVM, so it doesn't require to create an object to
invoke the main method. So it saves memory.
void is the 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. Here, System is a class, out is the object of
PrintStream class, println() is the method of PrintStream class.

JAVA SCANNER CLASS:

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

nextInt() reads an int value from the user


nextFloat() reads a float value form the user
nextBoolean() reads a boolean value from the user
nextLine() reads a line of text from the user
next() reads a word from the user
nextByte() reads a byte value from the user
nextDouble() reads a double value from the user
nextShort() reads a short value from the user
nextLong() reads a long value from the user
Example 1: Read a Line of Text Using Scanner
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
// creates an object of Scanner
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
// takes input from the keyboard
String name = input.nextLine();
// prints the name
System.out.println("My name is " + name);
// closes the scanner
input.close();
}
}

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

public class Demo


{
public static void main(String[] args)
{
int num = 10, count, total = 0;
for(count = 1; count <= num; count++)
{
total = total + count;
}
System.out.println("Sum of first 10 natural numbers is: "+total);
}
}

4. To calculate area of triangle.

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);
}
}

You might also like