0% found this document useful (0 votes)
19 views49 pages

Introduction To Java

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
19 views49 pages

Introduction To Java

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 49

Java fundamentals

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 package declaration is optional. It is placed just after the documentation


section. In this section, we declare the package name in which the class is
placed. Note that there can be only one package statement in a Java
program. It must be defined before any class and interface declaration.
• We use the keyword package to declare the package name.
1.package javatpoint; //where javatpoint is the package name
2.package com.javatpoint; //
where com is the root directory and javatpoint is the subdirectory
A package in Java is a mechanism for organizing related
classes, interfaces, and resources into a single unit.
Import Statements

• The package contains the many predefined classes and interfaces. If


we want to use any class of a particular package, we need to import
that class.
• We use the import keyword to import the class. It is written before the
class declaration and after the package statement.
• In a Java program, we can use multiple import statements. For
example:
1.import java.util.Scanner; //it imports the Scanner class only
2.import java.util.*; //it imports all the class of the java.util package
Interface Section

• It is an optional section. We can create an interface in this section if required.


We use the interface keyword to create an interface. An interface is a slightly
different from the class. It contains only constants and method declarations.
Another difference is that it cannot be instantiated. We can use interface in
classes by using the implements keyword. An interface can also be used with
other interfaces by using the extends keyword. For example:
interface car
{
void start();
void stop();
}
Class Definition

• It is vital part of a Java program. Without the class, we cannot create


any Java program.
• A Java program may contain more than one class definition. We use
the class keyword to define the class. The class is a blueprint of a Java
program. It contains information about user-defined methods,
variables, and constants. Every Java program has at least one class that
contains the main() method.
class Student //class definition
{
}
Class Variables and Constants

• , 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:

public static void main(String args[])

}
• 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.

• System.out.println(); This statement is used to print text on the


screen as output, where the system is a predefined class, and out is an
object of the PrintWriter class defined in the system. The method println
prints the text on the screen with a new line. You can also use print()
method instead of println() method. All Java statement ends with a
semicolon.
public class Student //class definition
{
public static void main(String args[])
{
//statements
}
}
Methods and behavior

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

• Portable: Java Byte code can be carried to any platform.


• High Performance: Java is an interpreted language. Java enables high performance with the use
of the Just-In-Time compiler.
• Multi-threaded: Java also supports multi-threading. It means to handle more than one job a time.
• Distributed: Java also has networking facilities. It is designed for the distributed environment of
the internet because it supports TCP/IP protocol
Parameters used in First Java Program

• class keyword is used to declare a class in Java.


• public keyword is an access modifier that 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.
• 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 or String args[] is used for command line argument. We will discuss it
in coming section.
• System.out.println() is used to print statement. Here, System is a class, out is an
object of the PrintStream class, println() is a method of the PrintStream class
Java Variables and Literals

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

int speedLimit = 80;


... .. ...
speedLimit = 90;
• Here, initially, the value of speedLimit is 80. Later, we changed it
to 90.
• However, we cannot change the data type of a variable in Java within
the same scope.
• int speedLimit = 80;
• float speedLimit;
Rules for Naming Variables in Java

• Java is case sensitive. Hence, age and AGE are two different variables.
int age = 24;int AGE = 25;

• Variables must start with either a letter or an underscore, _ or


a dollar, $ sign. int age; int _age;int $age;

• Variable names cannot start with numbers. int 1age;

• Variable names can't use whitespace. int my age;


Java data types

• Data types specify the type of data that can be stored inside variables in Java.

• There are two types of data types in Java:

1.Primitive data types: The primitive data types include boolean, char, byte, short,
int, long, float and double.

2.Non-primitive data types: The non-primitive data types include Classes,


Interfaces, and Arrays.
boolean type

• The boolean data type has two possible values, either true or false.

• Default value: false.

• They are usually used for true/false conditions.

• Example 1: Java boolean data type.


class Main {
public static void main(String[] args) {

boolean flag = true;


System.out.println(flag); // prints true
}
}
2. byte type

• 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

• Example 3: Java short data type


class Main {
public static void main(String[] args) {

short temperature;
temperature = -200;
System.out.println(temperature); // prints -200
}
}
Java Operators

• Operators are symbols that perform operations on variables and values.

• Operators in Java can be classified into 5 types:

1. Arithmetic Operators

2. Assignment Operators

3. Relational Operators

4. Logical Operators

5. Unary Operators

6. Bitwise Operators
Java Arithmetic Operators

• Arithmetic operators are used to perform arithmetic operations on


variables and data.
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Operation (Remainder after division)
class Main {
public static void main(String[] args) {

// 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

• Assignment operators are used in Java to assign values


to variables.
class Main {
public static void main(String[] args) {

// create variables
int a = 4;
int var;

// assign value using =


var = a;
System.out.println("var using =: " + var);

// assign value using =+


var += a;
System.out.println("var using +=: " + var);

// assign value using =*


var *= a;
System.out.println("var using *=: " + var);
}
}
Java Relational Operators

• Relational operators are used to check the relationship


between two operands.
• It returns either true or false.
class Main {
public static void main(String[] args) {
int a = 7, b = 11;
System.out.println("a is " + a + " and b is " + b);
// == operator
System.out.println(a == b); // false
// != operator
System.out.println(a != b); // true
// > operator
System.out.println(a > b); // false
// < operator
System.out.println(a < b); // true
// >= operator
System.out.println(a >= b); // false

// <= operator
System.out.println(a <= b); // true
}
}
Java Logical Operators

• Logical operators are used to check whether an expression is true or


false. They are used in decision making.
class Main {
public static void main(String[] args) {

// && 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);

System.out.println("Value of b: " + b);

// decrement operator
result2 = --b;
System.out.println("After decrement: " + result2);
}
Constructors in Java

• In Java, a constructor is a block of codes similar to the method. It is called


when an instance of the class is created.

• At the time of calling constructor, memory for the object is allocated in the
memory.

• It is a special type of method which is used to initialize the object.

• Every time an object is created using the new() keyword, at least one
constructor is called.
Rules for creating Java constructor

1.Constructor name must be the same as its class name

2.A Constructor must have no explicit return type

3.A Java constructor cannot be abstract, static, final, and synchronized


Types of Java constructors

• Default constructor (no-arg constructor)

• Parameterized constructor
Java Default Constructor

• A constructor is called "Default Constructor" when it doesn't have any parameter.

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

public static void main(String args[]){


//creating objects
Student3 s1=new Student3();
Student3 s2=new Student3();
//displaying values of the object
s1.display();
s2.display();
}
}

Output:

0 null
0 null
Java Parameterized Constructor

• A constructor which has a specific number of parameters is called a parameterized

constructor.

• Why use the parameterized constructor?

• The parameterized constructor is used to provide different values to distinct objects.

However, you can provide the same values also.


/Java Program to demonstrate the use of the parameterized constructor.
class Student4{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n){
id = i;
name = n;
}
//method to display the values
void display(){System.out.println(id+" "+name);}

public static void main(String args[]){


//creating objects and passing values
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
//calling method to display the values of object
s1.display();
s2.display();
}
}
o/p
111 Karan

You might also like