0% found this document useful (0 votes)
3 views42 pages

Java Tutorial

Java is a widely-used programming language created in 1995, owned by Oracle, and runs on over 3 billion devices. It is versatile, supporting mobile, desktop, web, and enterprise applications, and is known for its platform independence, security, and community support. The document also covers Java's features, differences from C, variable types, data types, methods, and examples of basic Java programs.

Uploaded by

anair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views42 pages

Java Tutorial

Java is a widely-used programming language created in 1995, owned by Oracle, and runs on over 3 billion devices. It is versatile, supporting mobile, desktop, web, and enterprise applications, and is known for its platform independence, security, and community support. The document also covers Java's features, differences from C, variable types, data types, methods, and examples of basic Java programs.

Uploaded by

anair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

JAVA

TUTORIAL
What is Java?
• Java is a popular programming language, created in 1995.
• It is owned by oracle, and more than 3 billion devices run Java.
• It is used for :
1. Mobile applications (specially Android apps)
2. Desktop applications such as acrobat reader, media player, antivirus,etc.
3. Web applications such as irctc.co.in, javapoint.com, etc
4. Enterprise Applications such as banking applications.
5. Web servers and application servers
6. Games
7. Database connection
8. Smart Card
9. Embedded System
Why use Java?

• Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
• It is one of the most popular programming language in the world
• It is easy to learn and simple to use
• It is open-source and free
• It is secure, fast and powerful
• It has a huge community support (tens of millions of developers)
Features of Java
1. Simple 2. Object-Oriented

3. Portable 4. Platform independent

5. Secured 6. Robust

7. Architecture neutral 8. Interpreted

9. High Performance 10. Multithreaded

11. Distributed 12. Dynamic


Difference between C and JAVA
C JAVA
1. It is procedural Language 1. It is Object Oriented Language
2. It uses pointer 2. No use of pointer
3. Error crashes in C 3. Exception Handling in Java
4. It is platform dependent 4. It is platform independent
5. It Support Structure and Union 5. It support Classes
6. It does not support threads 6. It support threads
7. It has 32 keywords 7. It has 57 keywords
8. It supports user-based memory
management. 8. It internally manages the memory.
9. In C declaration of variables is at the 9. We can declare variables anywhere.
beginning of the block.
10. Default members of C are public. 10. Default members of Java are private.
Hello World Example
The requirement for Java Hello World Example
• For executing any java program, you need to Install the JDK if you don't have
installed it, download the JDK and install it.
• Set path of the jdk/bin directory.
http://www.javatpoint.com/how-to-set-path-in-java
• Create the java program
• Compile and run the java program

class Simple save this file as Simple.java


{
public static void main(String args[]) To compile: javac Simple.java
{ To execute: java Simple
System.out.println("Hello Java");
} Output:Hello Java
}
Parameters used in First Java Program
• 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. We will learn it later.
• 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. We will
learn about the internal working of System.out.println statement later.
Internal Details of Hello Java Program
What happens at compile time?
At compile time, java file is compiled by Java Compiler (It does not interact
with OS) and converts the java code into bytecode.
What happens at runtime?

Classloader: is the subsystem of JVM that is used to load class files.

Bytecode Verifier: checks the code fragments for illegal code that can
violate access right to objects.

Interpreter: read bytecode stream then execute the instructions.


Q) Can you have multiple classes in a java source file?
Q) Can you save a java source file by other name than the class name?
How to set path in Java
• The path is required to be set for using tools such as javac, java, etc
• If you are saving the Java source file inside the JDK/bin directory, the path is not
required to be set
• However, if you have your Java file outside the JDK/bin folder, it is necessary to
set the path of JDK.
• There are two ways to set the path in Java:
1. Temporary
2.Permanent
1) How to set the Temporary Path of JDK in Windows

• The path is required to be set for using tools such as javac, java, etc.
• To set the temporary path of JDK, you need to follow the following steps:
STEP 1: Open the command prompt
STEP 2: Copy the path of the JDK/bin directory
STEP 3: Write in command prompt: set path=copied_path
For Example:
set path=C:\Program Files\Java\jdk1.6.0_23\bin
2) How to set Permanent Path of JDK in Windows

• For setting the permanent path of JDK, you need to follow these steps:
• Go to MyComputer properties -> advanced tab -> environment variables -> new
tab of user variable -> write path in variable name -> write path of bin folder in
variable value -> ok -> ok -> ok
Difference between JDK, JRE, and JVM

JVM
• JVM (Java Virtual Machine) is an abstract machine.
• It is called a virtual machine because it doesn't physically exist.
• It is a specification that provides a runtime environment in which Java bytecode
can be executed.
• The JVM performs the following main tasks:
1) Loads code
2) Verifies code
3) Executes code
4) Provides runtime environment
JRE ( Java Runtime Environment)
• It is a set of software tools which are used for developing Java applications.
• It is used to provide the runtime environment.
• It is the implementation of JVM
• It physically exists.
• It contains a set of libraries + other files that JVM uses at runtime.
JDK(Java Development Kit)
• is a software development environment which is used to develop Java
applications and applets.
• It physically exists. It contains JRE + development tools.
Java Variables
• A variable is a container which holds the value while the java program is
executed.
• A variable is assigned with a datatype.
• Variable is a name of memory location.
• For example
int data=50;//Here data is variable

• Types of Variables
1) local variable
2) instance variable
3) static variable
Types of variables
1) Local Variable
• A variable declared inside the body of the method is called local variable.
2) Instance Variable
• A variable declared inside the class but outside the body of the method, is called
instance variable.
3) Static variable
• A variable which is declared as static is called static variable.
• It cannot be local.
• Memory allocation for static variable happens only once when the class is loaded
in the memory.
• You can create a single copy of static variable and share among all the instances
of the class.
• Example to understand the types of variable in java
class A{
int data=50;//instance variable
static int m=100;//static variable
void method(){
int n=90;//local variable
}
}//end of class
Static Variable example
Data Types in Java
• Data types specify the different sizes and values that can be stored in the
variable. There are two types of data types in Java:
• Primitive data types: The primitive data types include boolean, char, byte,
short, int, long, float and double.
• Non-primitive data types: The non-primitive data types include Classes,
Interfaces, and Arrays.
Java Primitive Data Types
Data Type Default Value Default size

boolean false 1 bit


char '\u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte
Q.1) WAP to swap two numbers.
ALGORITHM:
1. Start.
2. Define class Swap
3. Declare variable a, b and temp as integer.
4. Display the value of a & b before swapping.
5. Swap a & b.
temp <- a
a <- b
b <- temp
6. Display the value of a & b after swapping.
7. Stop.
import java.util.*;
class swap
{
public static void main(String args[])
{
int a,b,temp ;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number a and b: ");
a=sc.nextInt();
b=sc.nextInt();
System.out.println("The value of a & b before swapping is"+a+","+b);
temp =a;
a=b;
b=temp;
System.out.println("The value of a & b after swapping is"+a+","+b);
}
Q. 2) WAP to reverse a number.
import java.util.*;
class reverse {
public static void main(String args[]) {
int a,r,rev=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number: ");
int no=sc.nextInt();
a=no;
while(no>0)
{
r=no%10;
rev=rev*10+r;
no=no/10;
}
Q. 2) WAP to reverse a number.
class reverse {
public static void main(String args[]) {
int a,r,rev=0;
int no=1234
a=no;
while(no>0)
{ r=no%10;
rev=rev*10+r;
no=no/10; }
System.out.println("Reverse of"+a+"is: "+rev); } }
Q. 3) WAP to find a square, square root, and Cube of a given no.
import java.util.*;
class maths {
public static void main(String args[]) {
int no, s;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number: ");
no=sc.nextInt();
s=no*no;
System.out.println("The Square of"+no+"is:"+s);
System.out.println("The Square root of"+no+"is:"+Math.sqrt(no));
System.out.println("The Cube of"+no+"is:"+Math.pow(no,3)); } }
public class Main {
int x = 5;

public static void main(String[] args) {


Main myObj1 = new Main(); // Object 1
Main myObj2 = new Main(); // Object 2
System.out.println(myObj1.x);
System.out.println(myObj2.x);
}
}
Java Class Attributes
public class Main { public class Main {
int x = 10; final int x = 10;
public static void main(String[] args) public static void main(String[] args)
{ {
Main myObj = new Main(); Main myObj = new Main();
myObj.x = 25; // x is now 25 myObj.x = 25; // will generate an
System.out.println(myObj.x); error: cannot assign a value to a final
} variable
} System.out.println(myObj.x);
}
}
public class Main {
String fname = "John";
String lname = "Doe";
int age = 24;

public static void main(String[] args) {


Main myObj = new Main();
System.out.println("Name: " + myObj.fname + " " + myObj.lname);
System.out.println("Age: " + myObj.age);
}
}
Method in Java
• Access Specifier: Access specifier or modifier is the
access type of the method. It specifies the visibility of the
method. Java provides four types of access specifier:
• Public: The method is accessible by all classes when we
use public specifier in our application.
• Private: When we use a private access specifier, the
method is accessible only in the classes in which it is
defined.
• Protected: When we use protected access specifier, the
method is accessible within the same package or
subclasses in a different package.
• Default: When we do not use any access specifier in the
method declaration, Java uses default access specifier by
default. It is visible only from the same package only.
Types of Method
There are two types of methods in Java:
• Predefined Method
• User-defined Method
public class Demo
{
public static void main(String[] args)
{
// using the max() method of Math class
System.out.print("The maximum number is: " + Math.max(9,7
));
}
}

You might also like