Sei sulla pagina 1di 43

Java Basic --Sumit Kumar Chaudhary

Java Introduction
What is a Java ?
Java is programming language and computing platform. There are lots
of application and websites that will works unless you have a java
installed.
Is created by James Gosling
JDK vs JRE vs JVM
• JDK --- Java Development Kit
• If we want to write a code then we required JDK in our system.
• JDK provide a development kit for develop a program

• When you download JDK, JRE is also downloaded, and don't need to
download it separately. In addition to JRE, JDK also contains number of
development tools (compilers, Javadoc, Java Debugger etc.).
JDK vs JRE vs JVM
• JRE------ Java Runtime Environment
• JRE provide the environment to execute the code.
• That why we are call system independent programing language
• Simply if we have JDK in our system then we will execute program easily.
JDK vs JRE vs JVM
• JVM---- Java Virtual Machine
• That will actual run the code and it’ll provide compiler that call javac
• JVM (Java Virtual Machine) is an abstract machine that enables your
computer to run a Java program.
• When you run the Java program, Java compiler first compiles your Java
code to byte code. Then, the JVM translates byte code into native machine
code (set of instructions that a computer's CPU executes directly).
• Java is a platform-independent language. It's because when you write Java
code, it's ultimately written for JVM but not your physical machine
(computer). Since, JVM ​executes the Java byte code which is platform
independent, Java is platform-independent.
Java Complete Structure in the diagram
What is a class
• Class is a collection of variable or method
• Variable ---------which stores the value and value can be change data type
variable name
Example for define a variable
int integerValue; float floatValue; double doubleValue;
String StringValue; char charcterValue;
• Method-----is a actual logic of your need. Is a main method is the entry point
of execution
What is an object & why we are create ?
• Object is a state and behaviors
• Object is the core concept of object oriented programming language.
• Object is create for access a variable or methods.

How to Create an object ?

ClassName referenceVariable=new ClassName();

*New keyword is use for create an object


* We can write anything according to requirement/ situation in the place of ‘referenceVariable’
What is a variable ?
• A variable which can be store a value and value can be change.
• There two type of variable
• Global Variable --- A variable create out side of method. We can use this type
of variable easily with in a class or other class. We can call with help of
reference variable
• Int a;
• ClassName referenceVariable= New ClassName()
• referenceVariable.a;
• Local Variable----A variable create in side of method. We can use this type of
variable with in a method Or we cant user out of method.
What is a Method and define the kind of?
• Method is store actual logic
• Method is two type
• Default Method
• Parameterized method
public void abc()
{
//default method
}
public void abc1(int a, int b)
{
//Parameterized method
}
Void vs Non-void Method
• Void ---- void is not return type method
• void means this method doesn't return a value. Methods can return a single value in
Java and it has to be defined in the method declaration. However, you can use return
by itself to exit the method.
Void Method
public void sum( )
{
}
Not Void method
public int sum(int a, int b )
{
return a+b;
}
1. We have to break the things
2. There are two type of methods – default and parameterized method
3. If we are passing the inputs to the method that will be parameterized
4. if method is not returning any value then there will be void and if we nee the output value of
that method in that case it will be no void ad there will be return statement.
Final Keyword

• Final keyword is user for define constant value


• When we define any value to final then we cant not change this value
Scanner Class
• In java scanner is available in import java.util.*;
• Scanner class will be use taking input from user at run time.
• Scanner is a class which is given by java.
• When we want to input something in run time from the user then we
user System.in
• This help to take input from user side
Data type
• int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99f; // Floating point number
char myLetter = 'D'; // Character
double
boolean myBool = true; // Boolean
String myText = "Hello"; // String
Data type conversion
Implicit and explicit conversion
• Implicit – This complier is done by java complier
• Implicit conversion no need to user intervention
• Example : Int a =30 ; Float a;
// float have greater size then integer
• F=a; // we can store small value to large easily
• Explicit conversion
• When we convert the any value then this conversion call is explicit conversion
• Example : Float x=12.5F; int y;
• Y=(int) x;
• We need to type casting here so we can easily store large value to small
Operators
• Operators are special characters within the Java language to manipulate primitive
data types. Java operators can be classified as :
• Unary : Takes one argument. These operators appear before (prefix) its argument or
after (postfix) its argument.
• Binary : Takes two arguments. These operators appear between its arguments.
• Ternary : Takes three arguments. These operators appear between its arguments.

• Different types of Operators in java
• Assignment Operators : =
Arithmetic Operators : - + * / % ++ --
Relational Operators : > < >= <= == !=
Logical Operators : && || & | ! ^
Conditional Operator : ?
Bit wise Operator : & | ^ >> >>>
Compound Assignment Operators : += -= *= /= %=
Assignment
Operator
• Assignment
Operator is denoted
by the symbol “=” and
it is the most
commonly used
operator. it simply
assigns the value
on the right to the
variable on the left.
Syntax of
the assignment
operator is <variable>
= <expression>.
Arithmetic Operators
• Arithmetic operators perform the same basic operations you would
expect if you used them in mathematics. They take two operands and
return the result of the mathematical calculation. There are seven
important arithmetic operators available in Java:
• Addition ‘+’ : This add two numbers or concatenate two strings
Subtraction ‘-‘ : This subtracts right side operand from the left side
operand
Multiplication ‘*’ : This multiplies two numbers
Division ‘/’ : This divides left side operand by the right side operand
Modulo ‘%’ : This divides left side operand by the right side operand
and returns remainder
Increment ‘++’ : This increases the value by 1
Decrement ‘- -‘ : This decreases the value by 1
Constructor
• A constructor is a special method that is used to initialize objects.
The constructor is called when an object of a class is created.
• Note that the constructor name must match the class name, and it
cannot have a return type (like void).
• All classes have constructors by default: if you do not create a class
constructor yourself, Java creates one for you. However, then you are
not able to set initial values for object attributes
Relational Operators
• Relational Operators are used to determine the comparison between two or
more objects. These operators always return the boolean value either true or
false when used in an expression. In java we have six different relational
operators:
• Greater than ‘>’ : This checks if the value of left operand is greater than value
of right operand
Less than ‘<‘ : This checks if the value of left operand is less than the value of
right operand
Greater than or Equal to ‘>=’ : This checks if the value of left operand is
greater than or equal to the value of right operand
Less than or Equal to ‘<=’ : This checks if the value of left operand is less than
or equal to the value of right operand
Equal ‘==’ : This checks if the value of both operands are equal
Not Equal ‘!=’ : This checks if the value of two operands are not equal
Logical Operators

• Logical operators return a true or false value based on the state of


the Variables. Each argument to a logical operator must be a boolean
data type, and the result is always a boolean data type. Below are
the three most commonly used logical operators:
• And Operator ‘&&’ : This returns true if the output of both the
operands are true
OR Operator ‘||’ : This returns true if the output of either operands
are true
NOT Operator ‘!’ : This invert the state of the condition
Conditional Operator

• The conditional operator is the only operator that takes three


arguments in Java. The conditional operator is equivalent to if-else
statement. It is also known as the ternary operator. This operator
consists of three operands and is used to evaluate Boolean
expressions. The goal of the operator is to decide which value should
be assigned to the variable. The operator is written as:
• variable = (expression) ? value if true : value if false
Kind of constructor
• Default constructor
• Parameterized constructor

• Example
Public ClassA
{
public ClassA(){}// this is default constructor
public ClassA(int a){} // parameterized constructor
}
This is constructor
example with this
keyword
This keyword is user for
call another
constructor with
In the class
Constructor Second example with void methods
Extends and super() keyword
• - Extends is user for inherit the parent class class method to child class
• super() keyword is similar to this() keyword but
• super() keyword is user for call another class constructor

this() keyword--- is use for call a constructor with in class


- this() keyword must write in first line
super() keyword--- is use for another class constructor
- super() keyword we can write anywhere in class
- super() is help to drive all parent class method in child class
Parent
class with
constructor
Child class extends to parents class
File handling
Filereader class
Java have File class in IO package
- File class- it is used for making connection/ tunnel.
- FileReader it is class - it is use for read a data character by character.
fileReader required a file class object because file class’s
object content a file path.
.read() it is method drive from fileReader class. It return integer
value. The character read, or -1 if the end of the stream has been
reached. -1 means end of file.
Every character have an ascii value available in ascii table . And the
ascii value its available in decimal value than the compiler convert the
decimal value in binary value after that save in system.
Bufferedreader
- Bufferedreader is a class available in IO package. It appending the
character and return the sentence line by line. It depend on fileReader
class.
- readLine() is method drive from bufferedReader class.A String
containing the contents of the line, not including any line-termination
characters, or null if the end of the stream has been reached without
reading any characters

Potrebbero piacerti anche