0% found this document useful (0 votes)
25 views

Basic of Java PDF

Uploaded by

Madhuri Desai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Basic of Java PDF

Uploaded by

Madhuri Desai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Object-Oriented Programming in Java

Introduction to Java

History of Java

 Java is one of the most popular programming languages because it is


used in various tech fields like app development, web development,
client-server applications, etc.
 Java is an object-oriented programming language developed by Sun
Microsystems of the USA in 1991.
 It was originally called Oak by James Goslin. He was one of the
inventors of Java.
 Java = Purely Object-Oriented.

 Java applications:

A. Web Application:
Web applications are the applications that run on web browser using servlet, JSP, struts
technologies. These technologies create java web applications and deploy them on server.
B. Mobile Application:
These are mobile applications created using java.
C. Standalone Application:
Standalone applications are executed by themselves without the need of other programs and
files. Example of such an application is antivirus.
D. Enterprise Application:
Some applications are designed for corporate organizations with the intent to control major
process in real time. Such applications are called enterprise applications.

1
Prof. Madhuri V. Desai
Object-Oriented Programming in Java

 Features of Java :

There is given many features of java. They are also known as java buzzwords. The Java
Features given below are simple and easy to understand.

 Object Oriented: In object oriented programming everything is an object rather that


function and logic.
 Simple: Java is simple to understand, easy to learn and implement.
 Secured: It is possible to design secured software systems using Java.
 Platform Independent: Java is write once and run anywhere language, meaning once
the code is written, it can be executed on any software and hardware systems.
 Portable: Java is not necessarily fixated to a single hardware machine. Once created,
java code can be used on any platform.
 Architecture Neutral: Java is architecture neutral meaning the size of primitive type is
fixed and does not vary depending upon the type of architecture.
 Robust: Java emphasizes a lot on error handling, type checking, memory
management, etc. This makes it a robust language.
 Interpreted: Java converts high-level program statement into Assembly Level
Language, thus making it interpreted.
 Distributed: Java lets us create distributed applications that can run on multiple
computers simultaneously.
 Dynamic: Java is designed to adapt to ever evolving systems thus making it dynamic.
2
Prof. Madhuri V. Desai
Object-Oriented Programming in Java

 Multi-thread: multi-threading is an important feature provided by java for creating


web applications.
 High-performance: Java uses Just-In-Time compiler thus giving us a high
performance.

How Java Works?


The source code in Java is first compiled into the bytecode.
Then the Java Virtual Machine(JVM) compiles the bytecode to the machine code.

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. It can also run those programs which are written in other languages
and compiled to Java bytecode.

The JVM performs the following main tasks:

o Loads code
o Verifies code
o Executes code
o Provides runtime environment
3
Prof. Madhuri V. Desai
Object-Oriented Programming in Java

JRE
JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. The Java
Runtime Environment 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
JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a software
development environment which is used to develop Java applications and applets. It physically
exists. It contains JRE + development tools. The JDK contains a private Java Virtual Machine
(JVM) and a few other resources.

4
Prof. Madhuri V. Desai
Object-Oriented Programming in Java

Java Program Structures


Simple Java Program

class Example
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}

Description of the every line of the program :

1. The first line contains the keyword class and class name, which actually the basic
unit for encapsulation, in which data and methods are declared.

2. Second line contains "{" which indicates the beginning of the class.

3. Third line contains the


public static void main(String args[])

where public is access specifier, when a member of a class is made public it


can be accessed by the outside code also. The main function is the beginning of
from where execution starts. Java is case-sensitive. “Main” is different from the “main”.
In main there is one parameter, String args, which is used to read the command line
arguments.

4. Fourth line contains the "{", which is the beginning of the

main function.

5
Prof. Madhuri V. Desai
Object-Oriented Programming in Java

5. Fifth line contains the statement

System.out.println("Hello World");

Here "System" is the predefined class, that provides access to the system, and out is the
output stream that is used to connect to the console. The println(), is used to display string
passed to it. This can even display other information to.

 Java Comments
Comments in any programming language are ignored by the compiler or the
interpreter. A comment is a part of the coding file that the programmer does
not want to execute, rather the programmer uses it to either explain a block
of code or to avoid the execution of a specific part of code while testing.

There are two types of coments:

1) Single-line comment
2) Multi-line comment

 Single Line Comments:

To write a single-line comment just add a ‘//’ at the start of the line.

Example:
class simple
{
public static void main(String[] args) {
//This is a single line comment
System.out.println("Hello World!!!");
}
}

6
Prof. Madhuri V. Desai
Object-Oriented Programming in Java

 Multi Line Comments:

To write a multi-line comment just add a ‘/*…….*/’ at the start of the line.
Example:

class simple

public static void main(String[] args)

/*This

is

Multiline

Comment

*/

System.out.println("Hello World!!!");

}
}

7
Prof. Madhuri V. Desai
Object-Oriented Programming in Java

Variables in Java Programming

 Variables

 A variable is a container that stores a value.


 This value can be changed during the execution of the program.
 Example: int number = 8; (Here, int is a data type, the number is the variable name,
and 8 is the value it contains/stores).

Rules for declaring a variable name


We can choose a name while declaring a Java variable if the following rules are followed:

 Must not begin with a digit. (E.g., 1arry is an invalid variable)


 Name is case sensitive. (Harry and harry are different)
 Should not be a keyword (like Void).
 White space is not allowed. (int Code With Harry is invalid)
 Can contain alphabets, $character, _character, and digits if the other conditions are
met.

 Literals

A literal is a value that can be passed to a variable or constant in a program. Literals


can be numeric, boolean, character, string notation or null. A constant value can be
created using a literal representation of it. Here are some literals:

Interger literal Character literal Floating point literal byteliteral


int x=25; char ch=88; flaot f=12.34 byte b=12;

Java Variables
Variables are containers that store information that can be manipulated and referenced later by
the programmer within the code. Java variables have a data type associated with them which can
tell us about the size and layout of the variable’s memory.

8
Prof. Madhuri V. Desai
Object-Oriented Programming in Java

There are three types of variables in java:

 Local variable
 Instance variable
 Class/Static variable

 Local Variables:
A variable that is declared inside the body of the method or constructor is called a local
variable. It is called so because the extent of a local variable lies only within the method
or constructor within which it is created and other methods in the class cannot access it.
Inside the method body, local variable is declared using the static keyword.

Example:

class variableType

public void localVariable()

String name = "Ram";

int marks = 95;

System.out.println(name + " Scored " + marks + "%.");

public static void main(String[] args)

variableType v = new variableType();

v.localVariable();

9
Prof. Madhuri V. Desai
Object-Oriented Programming in Java

 Instance Variables:
An instance variable is declared inside the class but outside a method or a constructor. It is
similar to a static variable except that it is declared without using the keyword static. These
variables are accessible by all methods or constructors that are inside the class.

Example:

class variableType
{
String name = "Ram";
int marks = 95;
void instanceVariable()
{
System.out.println(name + " Scored " + marks + "%.");
}
public static void main(String[] args)
{
variableType v = new variableType();
v.instanceVariable();
}
}

 Static Variables:
An static variable is declared inside the class but outside a method or a constructor. It is similar
to a instance variable except that it is declared using the keyword static. These variables are
accessible by all methods or constructors that are inside the class.

Example:

class variableType
{

static String name;

10
Prof. Madhuri V. Desai
Object-Oriented Programming in Java

static int marks;

Public static void main(String[] args)


{
Name = “Ram”;
Marks = 95;
System.out.println(name + “ Scored “ + marks + “%.”);
}
}

Difference between static variable


and instance variable
Let’s discuss the difference between the static and non-static
variables in java because non-static variables are instance
variables. This is known as the difference between class
variable and instance variable.

Instance variables

1. When we declare a global variable without a static


keyword that is known as an instance variable. We don’t use
the static keyword with instance variables. Because the
instance variable binds with the instance/object of the class.
2. When we create an instance/object of a class then JVM
creates a copy of instance variables associated with the
corresponding object. The instance variable gets destroyed when
the instance/object is destroyed.

3. The instance variables are accessible through the object.

4. In non-static methods, Instance variables are accessible


directly by name within the class. But in the static method, we
have to use the object name.
11
Prof. Madhuri V. Desai
Object-Oriented Programming in Java

5. Instance variables are not common for all objects because


JVM creates a separate copy of each instance variable when we
create an object.

Class variables(static variables)

1. When we declare a global variable with the static keyword that


is known as class variable/ static variable. Class variables are
also known as static variables because we use static keywords.
These variables directly bind to class instead of instance/object.

2. But static variables load in memory during the class loading.


When JVM loads the class in memory then a static variable gets
created in Static pool memory. The static variables get
destroyed when the program stops.

3. The static variables are accessible directly by class name.

4. In static or non-static methods, we can directly access the


static variables by the name of the variable within the same
class.

5. But static variables are standard(Common) for all objects of


class because they bind with class, not instance/object.

 Data Types in Java Programming

12
Prof. Madhuri V. Desai
Object-Oriented Programming in Java

Data types in Java fall under the following categories

1. Primitive Data Types (Intrinsic)


2. Non-Primitive Data Types (Derived)

Primitive Data Types


Java is statically typed, i.e., variables must be declared before use. Java supports 8 primitive
data types:

Data
Size Value Range
Type

Byte 1 byte -128 to 127

short 1 byte -32,768 to 32,767

-2,147,483,648 to
Int 2 byte
2,147,483,647

3.40282347 x 1038 to
Float 4 byte
1.40239846 x 10-45

-9,223,372,036,854,775,808
Long 8 byte
to 9,223,372,036,854,775,807

1.7976931348623157 x 10308,
double 8 byte
4.9406564584124654 x 10-324

Char 2 byte 0 to 65,535

Depends
boolean True or False
on JVM

13
Prof. Madhuri V. Desai
Object-Oriented Programming in Java

 Getting user input in Java


Reading data from the Keyboard :
To use the Scanner class, we need to import the java.util.Scanner package.
import java.util.Scanner;
Now the Scanner class has been imported. We create an object of the Scanner class to use the
methods within it.
Scanner sc = new Scanner(System.in)
The System.in is passed as a parameter and is used to take input.

different methods to take inputs


int a = Sc.nextInt(); // Taking an integer input from the keyboard
float a = sc.nextFloat(); // Taking floating input from the keyboard
String str = sc.next(); // Taking string input from the keyboard

Below are the methods that belong to the scanner class:


14
Prof. Madhuri V. Desai
Object-Oriented Programming in Java

Method Description
nextLine() Accepts string value
next() Accept string till whitespace
nextInt() Accepts int value
nextFloat() Accepts float value
nextDouble() Accepts double value
nextLong() Accepts long value
nextShort() Accepts short value
nextBoolean() Accepts Boolean value
nextByte() Accepts Byte value

Example
import java.util.Scanner;
class Example
{
Public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println(“Enter Name, RollNo”);
String name = s.nextLine(); //used to read line
Int RollNo = s.nextInt(); //used to read int
System.out.println(“Name:” +name);
System.out.println(“RollNo” +RollNo);
S.close();
}
}

15
Prof. Madhuri V. Desai
Object-Oriented Programming in Java

Program 1.
Write a program to calculate the percentage of a given student in board exam. His marks from 5
subjects must be taken as input from the keyboard. (Marks are out of 100)

import java.util.Scanner;

public class Result


{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter your Physics marks : ");
int physics = scan.nextInt();
System.out.println("Enter your English marks : ");
int English = scan.nextInt();
16
Prof. Madhuri V. Desai
Object-Oriented Programming in Java

System.out.println("Enter your Chemistry marks : ");


int chemistry = scan.nextInt();
System.out.println("Enter your Mathematics marks : ");
int mathematics = scan.nextInt();
System.out.println("Enter your Computer Science marks : ");
int computer = scan.nextInt();
float percentage = ((physics + English + chemistry + mathematics + computer)/500.0f)*100;
System.out.println("percentage : ");
System.out.println(percentage);
}
}

17
Prof. Madhuri V. Desai

You might also like