Sunday, 20 September 2015

To Check Prime Number

package com.tutorial4u;

import java.util.Scanner;

public class PrimeNumber {
       public static void main(String[] args) {
             int temp ;
             boolean isPrimeNumber = true;
             System.out.print("Please enter a number to check prime : ");
             Scanner sc = new Scanner(System.in);
             int num = sc.nextInt();
             for(int i=2;i<num/2;i++){
                    temp = num%i;
                    if(temp==0){
                           isPrimeNumber = false;
                           break;
                    }
             }
             if(isPrimeNumber){
                    System.out.println(num +" is Prime Number");
             }else{
                    System.out.println(num +" is not Prime Number");
             }
       }

}

Output:


Please enter a number to check prime : 23
23 is Prime Number

Palindrome Number



package com.tutorial4u;

import java.util.Scanner;

public class PalindromeNumber {
       public static void main(String[] args) {
             int num, reverse=0,temp;
             System.out.print("Please enter any number to check palindrome : ");
             Scanner sc = new Scanner(System.in);
             num = sc.nextInt();
            
             temp = num;
             while(num!=0){
                    reverse = reverse*10;
                    reverse=reverse+num%10;
                    num= num/10;
             }
             if(temp == reverse){
                    System.out.println("Given number is palindrome");
             }else{
                    System.out.println("Given number is not palindrome");
             }
       }

}

Output:

Please enter any number to check palindrome : 121

Given number is palindrome

Even Or Odd Number Without (%,/) Operator

package com.tutorial4u;

import java.util.Scanner;

public class EvenOrOdd {
       public static void main(String[] args) {
             System.out.print("Enter a num to check even or odd : ");
             Scanner sc = new Scanner(System.in);
             int num = sc.nextInt();
             if((num&1) == 0){
                    System.out.println("Given number is Even ");
             }else{
                    System.out.println("Given number is Odd");
             }
       }

}

Output: 

Enter a num to check even or odd : 34
Given number is Even 


Wednesday, 12 August 2015

Object and Class

Object and Class

OOPS (Object-Oriented Programming System) is a programming paradigm that solves problems by organizing code around objects, which represent real-world entities. It uses real-world concepts to model problems and solutions.

Main OOP Concepts:

  • Inheritance
  • Polymorphism
  • Encapsulation
  • Abstraction

Class : 

A class is declared using the class keyword. It acts as a blueprint that defines the structure and behavior (data and methods) of objects. 

A class can contain:

  • Fields (variables)
  • Methods
  • Constructors
  • Blocks (static or instance)
  • Nested classes and interfaces
Syntax:

class ClassName {  
    // fields  
    // methods  
}                                

Object :

An object is an instance of a class that represents a real-world entity. It has three main characteristics:

  • State → represents data or properties of the object.
  • Behavior → represents functionality (methods) of the object.
  • Identity → represents a unique reference to the object in memory.

Ways to Create/Initialize an Object in Java :

1. Using new keyword

MyClass obj = new MyClass();   
2.Using Reflection (Class.forName(), getDeclaredConstructor().newInstance())

MyClass obj = (MyClass) Class.forName("MyClass").getDeclaredConstructor().newInstance();  

3.Using clone() method

MyClass obj2 = (MyClass) obj1.clone();   
4. Using Deserialization

(Object is recreated from a saved state).

For more details click 👉here 

A class is initialized in Java when:

  1. An instance of the class is created using the new operator.
  2. A static method of the class is invoked.
  3. A static field of the class is accessed or assigned (unless it’s a compile-time constant).
  4. Reflection is used (e.g., Class.forName("MyClass")).
Example :

class MyClass {
    static {
        System.out.println("Class is being initialized!");
    }

    MyClass() {
        System.out.println("Constructor called: Object created");
    }

    static void staticMethod() {
        System.out.println("Static method called");
    }
}

public class TestInitialization {
    public static void main(String[] args) throws Exception {
        // Class initialization by creating object
        MyClass obj1 = new MyClass();

        // Class initialization by calling static method
        MyClass.staticMethod();

        // Class initialization using reflection
        MyClass obj2 = (MyClass) Class.forName("MyClass").getDeclaredConstructor().newInstance();
    }
} 
Output:


Class is being initialized! Constructor called: Object created Static method called Constructor called: Object created

Explanation:

  • The static block executes once when the class is initialized.
  • Class is initialized when an object is created, a static method is called, or using reflection.
  • Each new object triggers the constructor but does not re-run the static block.

Thursday, 2 October 2014

StaticBlock Program


class StaticBlock
{
 /*public static void main (String[] args)
 {
  System.out.println("Main Method is executed");
 }*/
 static
 {
  System.out.println("static method is executed");
  System.exit(0);
 }


Java Development Kit (JDK) and Java Runtime Environment (JRE)

                  Java Development Kit (JDK) and Java Runtime Environment (JRE)  To download and install the Java Development Kit (  JDK ) ...