JavaPrograms PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Learn Java Coding

INDEX
1. BasicPrograms
2. DataTypePrograms
3. VariablePrograms
4. OperatorPrograms
5. AccessSpecifierProgams
6. ConditionalStatementPrograms
7. LoopProgams
8. JumpStatementPrograms
9. ArrayPrograms
10. StringPrograms
11. OopsProgarams
12. ExecptionHandlingPrograms
13. CollectionPrograms
14. ThreadPrograms
15. JavaFilesAndInputOutputProgram
16. MiscallaneousPrograms
17. PatternPrograms
18. ConversionPrograms
19. CalculationPrograms
20. DataStucturePrograms
21. SortingPrograms
Learn Java Coding
My First Example Program
class MyFirstJavaProgram{
public static void main(String[] args){
System.out.println("My First Java
Program");
}
}

Output is:
My First Java Progra

#BasicPrograms

Constructor Example Java Program


Definition
A constructor in a class is a special type of method called to create an object, It has the same
name as that of the Class.

Constructor Types
• Default Constructor
• Parameterized Constructor
• Private(SingleTon) Constructor

Constructor Characteristics In Java


• A constructor has the same name of the Class
• A constructor has no explicit return type
• A constructor is called automatically, to create instance an object.
• A constructor must be the same as the name of the class.
• Not like methods, constructors are not members of a class. we cannot call directly
• A constructor can be overloaded.
• A Constructor with no argument is called default Constructor
• A Constructor with arguments is called Parameterized Constructor
• Private Constructor is used for Singleton Class
Syntax
NameOfClass() {
//Statements
}

Syntax Example
Class Constructor {
Constructor(){
System.out.print("Its Called");
}
}

Syntax Explanation
• In the example, the name of the constructor is the same as that of the class name.
When the constructor is executed, "I am inside default constructor" will be printed.

Constructor Example Program


class Constructor {
Constructor() {
System.out.println("I am inside default constructor");
}

public static void main(String[] args) {


Constructor obj = new
Constructor();
}
}

Output is:
I am inside default construct

Parametrized Method Example Java


Program
Definition
A method (or message) in object-oriented programming (OOP) is a procedure associated with
an object class. An object is made up of behaviour and data. Data is represented as properties
of the object and behavior as methods. Methods are also the interface an object presents to
the outside world. A parameterised method holds variables.

Syntax
access_specifier return_type method_name(data_type variable_name,
data_type variable_name){
//Statements
}

Parametrized Method Example Program


public class ParametrizedMethod{
public static void main(String[] args) {
int num1 = 11;
int num2 = 6;
int num3 = minValue(num1, num2);
System.out.println("Minimum Value while comparison of num 1 and num2
is = " + num3);
}
public static int minValue(int i, int j)
{ int min;
if (i > j){
min = j;
}
else{ min = i;

return min;
}
}

Output is:
Minimum Value while comparison of num1 and num2 is = 6

Static Class and Its Usage in Java Example Program

Static Class (Nested) Characteristics In Java


• A static class can access without creating any of object If it has static data members and
methods.
• A static class cannot access non-static data members and methods.
• The static class can be accessed by outer class name.
• Need to create an object for access non-static data members and methods.
• A static class created inside a class is called nested static class.
• Only nested classes can be static classes.
• In Java, we can declare a class within another class. It is called a nested class. That
nested class accessed based on Outer class.

Syntax
//Static Class
static class className(){
//Do something
}

//Static variable
static int variableName = 0;

//Static method inside a static class


static class className(){
static void staticMethod(){
//Do something
}
}

Static Class Example Program


class StaticClass {

public static class StaticClassInner {


public static int staticNumber = 100;

public static int staticMethod() {


staticNumber = 200;
return staticNumber;
}
}

public static void main(String []args){


System.out.println("Initial value of Static number : " +
StaticClassInner.staticNumber);
System.out.println("Static number inside static class : " +
StaticClassInner.staticMethod());
}
}

Sample Output
Initial value of Static number : 100
Static number inside static class : 200

Static Function or Method and Its Usage in Java Example


Program
Static Function Overview
• Static method or variable belongs to the class and not to object.
• Static method or variable can be used to all Objects created from the same class.
• Static variables initialized only once in the time of class loading.
• It Can use Static method or variable without instance creation.
• Static methods can be overloaded.But can not be overridden.

Syntax
class ClassName{
public <ReturnType> static methodName() {
//Do Something
}
}

Static Function or Method and Its Usage in Java Example


Program
public class StaticMethodAndUsages {
public static int staticMethod(){
int num1 = 100, num2 = 200,result;
result = num1+num2;
return result;
}
public static void main(String[] args) {
System.out.println("Result from static method is : "+staticMethod());
}
}

Sample Output
Result from static method is : 300

Default and Parameterized Constructor Java Example


Program
Definition
Default Constructor: A Constructor with no argument is called default Constructor
Parameterized Constructor: A Constructor with arguments is called Parameterized
Constructor

Syntax
//Default Constructor
Class ConstructorExample {
ConstructorExample (){
//Do something
}
}

//Parameterized Constructor
Class ConstructorExample {
<datatype> variableName;

ConstructorExample (<datatype> variableName){


this.variableName = variableName;
//Do something
}
}

Default and Parameterized Constructor Example Program


public class DefaultAndParameterizedConstructor {
public static void main(String[] args) {
//Creating object of class using default constructor
DefaultConstructorExample object1 = new DefaultConstructorExample();
object1.doAddition();

//Creating object of class using parameterized constructor


ParameterizedConstructorExample object2 = new
ParameterizedConstructorExample(100,200);
object2.doAddition();
}
}

class DefaultConstructorExample{
int num1 = 5, num2 = 10, result;
int doAddition(){
result = num1+num2;
System.out.println("This method is called using a default constructor");
return result;
}
}
class ParameterizedConstructorExample{
int num1, num2, result;

public ParameterizedConstructorExample(int num1, int num2) {


this.num1 = num1;
this.num2 = num2;
}

int doAddition(){
result = num1+num2;
System.out.println("This method is called using a parameterized
constructor");
return result;
}
}

Sample Output
This method is called using a default constructor
This method is called using a parameterized constructor

Constructor Chaining Java Example Program


Definition
Constructor chaining is calling a constructor from the another constructor of the same class.

Chaining Constructor Characteristics In Java


• Call another constructor using this() keyword in the same class.
• The class has two or more constructors.
• Each constructor has various type of arguments.

Syntax
class ClassName{

public ClassName() {
//Do Something
}

public ClassName(String string) {


//Calling the Constructor without any parameters - Chaining
this();
//Do something
}
}

Constructor Chaining Example Program


public class ConstructorChaining {
public static void main(String[] args) {
ChainingClass object = new ChainingClass("This is the third ", "Chaining
Constructor");
}
}

class ChainingClass{
public ChainingClass() {
System.out.println("This is the first Chaining Constructor");
}

public ChainingClass(String string) {


//Calling the Constructor without any parameters
this();
System.out.println(string);
}

public ChainingClass(String string1, String string2) {


//Calling the constructor with one parameter - Chaining
this("This is the second Chaining Constructor");
System.out.println(string1+string2);
}
}

Sample Output
This is the first Chaining Constructor
This is the second Chaining Constructor
This is the third Chaining Constructor

Singleton Class Using Private Constructor Java Example


Program
Syntax
class ClassName
{
static ClassName instance = null;

private ClassName() { }

static public ClassName getInstance()


{
if (instance == null)
instance = new Singleton();

return instance;
}
}

//Usage
ClassName instance = ClassName.getInstance();

Singleton Class Example Program


class Singleton
{
static Singleton instance = null;
public int instanceValue = 10;

private Singleton() { }

static public Singleton getInstance()


{
if (instance == null)
instance = new Singleton();

return instance;
}
}

public class SingletonClass {


public static void main(String args[])
{
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();

//Objects are different


//But same instance will be used
instance1.instanceValue = instance1.instanceValue + 10;
System.out.println("Value of instance1.instanceValue = " +
instance1.instanceValue);
System.out.println("Value of instance2.instanceValue = " +
instance2.instanceValue);
}
}

Sample Output
Value of instance1.instanceValue = 20
Value of instance2.instanceValue = 20

Constructor Overloading Example Java Program


Definition
A constructor in a class is a special type of subroutine called to create an object. It prepares
the new object for use, often accepting arguments that the constructor uses to set required
member variables. A constructor resembles an instance method, but it differs from a method
in that it has no explicit return type. Constructors often have the same name as the declaring
class.

Syntax
NameOfClass() {
//Statements
}
NameOfClass(Parameters) {
//Statements
}
ParentClass{
//Statements
}

Constructor Overloading Example Program


class ConstructorOverloading{
ConstructorOverloading(){
System.out.println("1. " + "Constructor with no parameter.");
}
ConstructorOverloading(int p, int q){
System.out.println("2. " + (p+q));
}
ConstructorOverloading(int p, int q, int r){
System.out.println("3. " + (p+q+r));
}
ConstructorOverloading(int p, int q, float r){
System.out.println("4. " + (p+q+r));
}
ConstructorOverloading(float r, int p, int q){
System.out.println("5. " + (p+q+r));
}
public static void main(String []args){
ConstructorOverloading obj = new ConstructorOverloading(10, 20);
ConstructorOverloading obj1 = new ConstructorOverloading(10, 20,
30);
ConstructorOverloading obj2 = new ConstructorOverloading();
ConstructorOverloading obj3 = new ConstructorOverloading(10, 20,
4.5f);
ConstructorOverloading obj4 = new ConstructorOverloading(7.8f,
10, 20);
}
}

Sample Output
Output is:
2. 30
3. 60
1. Constructor with no parameter.
4. 34.5
5. 37.8

Simple Class and Array of Object Java Example Program


Syntax
<custom_object>[] <array_name> = new <custom_object>[<array_length>];

Simple Class and Array of Object Example Program


public class SimpleClassAndArrayOfObject {
static Employee[] generateArray(){
Employee[] employees = new Employee[3];

Employee employee1 = new Employee("Ramesh", 25);


Employee employee2 = new Employee("Suresh", 21);
Employee employee3 = new Employee("Ganesh", 29);

employees[0] = employee1;
employees[1] = employee2;
employees[2] = employee3;

return employees;
}

static void printArray(Employee[] employees){


for (int i = 0; i < employees.length; i++){
System.out.println(employees[i].name+" : "+employees[i].age);
}
}

public static void main(String[] args) {


Employee[] employees = new Employee[3];
employees = generateArray();
System.out.println("The list of employees in format 'name : age' is");
printArray(employees);
}
}

class Employee{
String name;
int age;

public Employee(String name, int age) {


this.name = name;
this.age = age;
}
}

Sample Output
The list of employees in format 'name : age' is
Ramesh : 25
Suresh : 21
Ganesh : 29

Who need Compelete


java programs pdf pay 50rs

You might also like