UNIT-1
UNIT-1
JAVA
What is java
• There are many features of java. They are also known as java buzz
words.
simple
• According to sun,java language is simple because:
• Syntax is based on c++
• Removed many confusing and rarely-used features ex:explicit
pointers,operator overloading.
Object oriented
access_modifier class<class_name>
{
data member;
method;
constructor;
nested class;
interface;
}
class Student
{
int id;//data member
String name; //data member
}
1./Java Program to create and call a default constructor
2.class Bike1
3.{
4.//creating a default constructor
5. Bike1()
6. {
7. System.out.println("Bike is created");
8. }
9.//main method
10. public static void main(String args[])
11. {
12. //calling a default constructor
13. Bike1 b=new Bike1();
14. }
15.}
java Parameterized Constructor
There can be a lot of usage of Java this keyword. In Java, this is a reference
variable that refers to the current object.
Usage of Java this keyword
Valid: InValid:
• int a, b, c; int int =45;
• int a = 10, b = 10;
• byte B = 22;
• double pi = 3.14159;
• char a = 'a';
• There are three kinds of variables in Java -
• Local variables - Local variables are declared in
methods, constructors, or blocks.
• Instance variables - Instance variables are declared in
a class, but outside a method, constructor or any block.
• Class/Static variables - Class variables also known as
static variables are declared with the static keyword in a
class, but outside a method, constructor or a block.
Datatype
• A datatype represents the type of value stored in a
variable
• 1.Primitive datatypes
• 2.NonPrimitive datatypes
Integers
• long num1 = 0L;
• long num2 = 401L;
• long num3 = -3556L
Float
• It is a 32-bit, single-precision floating-point number.
• Float data type in Java stores a decimal value with 6-7
total digits of precision
• Default value- 0.0f
1.float height = 167.7f or
2.float height = 167.7F
Double
• The double data type is a double-precision floating-
point.
• Double data type stores decimal values with 15-16
digits of precision.
Default value:0.0d
1.double price = 987.90D or
2.double price = 987.90d or
3.double price = 987.90
Boolean
• Boolean data type represents only one bit of
information either true or false which is intended to
represent the two truth values of logic.
Syntax:
boolean booleanVar;
char
• The char data type is a 16-bit unsigned Java primitive data type.
• Note that char is an unsigned data type.
• Therefore, a char variable cannot have a negative value.
• The range of the char data type is 0 to 65535
Char grade=‘A’;
• System.out.println(grade);
• //Output: A
Type Casting/type conversion
Syntax:
data_type array_name[][]; (OR) data_type[][]
array_name;
Different approaches for Initialization of 2-D
array in Java:
class Example {
public static void main(String[] args)
{
int[][] scores = new int[2][2];
scores[0][0] = 15;
Output:
scores[0][1] = 23; scores[0][0] = 15
scores[0][1] = 23
scores[1][0] = 30; scores[1][0] = 30
scores[1][1] = 21
scores[1][1] = 21;
% remainder of division
1.public class OperatorExample
{
2.public static void main(String
OUTPUT
args[]){ 15
5
3.int a=10; 50
4.int b=5; 2
0
5.System.out.println(a+b);//15
6.System.out.println(a-b);//5
7.System.out.println(a*b);//50
8.System.out.println(a/b);//2
9.System.out.println(a%b);//0
10.}}
Relational Operator
• A relational operator compares two values and determines the
relationship between them.
Operator Description
> Check if operand on the left is greater than operand on the right
| Bitwise OR
^ Bitwise exclusive OR
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Logical Operator
• Java supports following 3 logical operator, Suppose a=1 and b=0;
|| Logical OR (a || b) is true
• int a,b;
• a=2;
• b=5;
Control statements
Selection statements
If Statements
Simple if
If else
If -else -if ladder
Nested if
Switch
Iteration statements
Do while
for
Jump statements
break
• This statement is used to jump out of a loop
• Break statement was previously used in switch-case statements
• The remaining statements which are after the break and within the
loop are skipped
break
Continue statement
The return statement
• the return statement is used for returning a value when
the execution of the block is completed.
Method overloading
• If a class has multiple methods having same name but
different in parameters, it is known as Method
Overloading.
Different ways to overload the
method
import java.io.*;
class Product {
public int multiply(int a, int b)
{
int prod = a * b;
return prod;
}
class GFG {
System.out.println(
"Product of the two integer value :" + prod1);
System.out.println(
"Product of the three integer value :" + prod2);
}
}
2) Method Overloading: changing data type of arguments
import java.io.*;
class Product {
public int Prod(int a, int b, int c)
{
int prod1 = a * b * c;
return prod1;
}
OUTPUT:
public double Prod(double a, double b, double c) Product of the three integer value :6
{ Product of the three double value :6.0
double prod2 = a * b * c;
return prod2;
}
}
class GFG {
public static void main(String[] args)
{
class Student {
// Method 1
public void StudentId(String name, int roll_no)
{
System.out.println("Name :" + name + " " + "Roll-No :" +
roll_no);
}
// Method 2
public void StudentId(int roll_no, String name)
{ OUTPUT:
// Again printing name and id of person
System.out.println("Roll-No :" + roll_no + " " + "Name :" + Name :karan Roll-No :1
name); Roll-No :2 Name :Kamlesh
}
}
// Class 2
// Main class
class GFG {
int a = 10;
int b = 20;
System.out.println("Value of a: " + a
+ " & b: " + b);
System.out.println("Value of a: " +
obj.a
+ " & b: " +
obj.b);
// Displaying values
// after calling the function
System.out.println("Value of a: " +
obj.a
+ " & b: " +
obj.b);
}
Recursion
• Recursion in java is a process in which a method calls
itself continuously. A method in java that calls itself is
called recursive method.
Syntax:
1.returntype methodname
()
2.{
3.//code to be executed
4.methodname();//
calling same method
5.}
• class Factorial
• {
• // this is a recursive function
• int fact(int n)
• {
• int result;
• if(n==1)
• return 1;
• result = n * fact(n-1);
• return result;
• } OUTPUT:
• } Factorial of 3 is 6
• class Recursion Factorial of 4 is 24
• { Factorial of 5 is 120
• public static void main(String args[])
• {
• Factorial f = new Factorial();
• System.out.println("Factorial of 3 is " + f.fact(3));
• System.out.println("Factorial of 4 is " + f.fact(4));
• System.out.println("Factorial of 5 is " + f.fact(5));
• }
• }
String class
• string is basically an object that represents sequence of
char values. An array of characters works same as Java
string.
• For example:
• char[] ch={'j','a','v','a','t','p','o','i','n','t'};
String s=new String(ch);
is same as:
String s="javatpoint";
String class
String is a sequence of characters. In java, objects of String are immutable which means a constant
and cannot be changed
once created.
Creating a String
There are two ways to create string in Java:
•String literal
String s = “java”;
•Using new keyword
String s = new String (“java”);
String Handling methods
• String toUpperCase() and toLowerCase() method
• The Java String toUpperCase() method converts this String into uppercase letter and
String toLowerCase() method into lowercase letter.
• The equals method can be used to compare whether two strings are having the same
content or not. If An example of using equals method is given below:
• The method startsWith() checks whether the String starts with the
letters passed as arguments and endsWith() method checks whether
the String ends with the letters passed as arguments.
• public class Stringoperation3
• {
• public static void main(String ar[])
• {
• String s="Sachin";
• System.out.println(s.startsWith("Sa"));//true
• System.out.println(s.endsWith("n"));//true
• }
• }
Output:
True
true
String charAt() Method
•The String class length() method returns length of the specified String.
• public class Stringoperation5
• {
• public static void main(String ar[])
• {
• String s="Sachin";
• System.out.println(s.length());//6
• }
• }
valueOf()
• The overloaded valueOf method can be used to convert primitive type
values like int, float, double etc to a String.
Syntax:
static String valueOf(int value)
• An example of valueOf method is given below:
• int x = 10;
• String str = String.valueOf(x);
• System.out.println(str);
• Above example prints 10.
indexOf()
• The indexOf method is used to find the position (number) of a
specified character in the invoking string.
• Syntax:
int indexOf(char c)
• An example of using indexOf method is given below:
String str1 = "hello world";
System.out.println(str1.indexOf('o'));
• Above example prints 4 which the index of first ‘o’ in the string “hello
world”.
• The overloaded indexOf method is used to find the position (number)
of a specified string in the invoking string.
• Syntax:
int indexOf(String str)
1.class StringBufferExample3{
2.public static void main(String args[]){
3.StringBuffer sb=new StringBuffer("Hello");
4.sb.replace(1,3,"Java");
5.System.out.println(sb);//prints HJavalo
6.}
7.}
Output:
HJavalo
StringBuffer delete() Method
• The delete() method of the StringBuffer
class deletes the String from the
specified beginIndex to endIndex.
1.class StringBufferExample4{ Output:
2.public static void main(String args[]) Hlo
{
3.StringBuffer sb=new StringBuffer("Hel
lo");
4.sb.delete(1,3);
5.System.out.println(sb);//prints Hlo
6.}
7.}
Replace()