├── .vscode └── settings.json ├── AbstMain.java ├── Add1.java ├── ArithmeticOperators.java ├── BooleanDeclaration.java ├── CharDeclaration.java ├── ClaMain.java ├── DoubleDeclaring.java ├── EncapMain.java ├── FaceMain.java ├── FloatMult.java ├── IntExample0.java ├── IntExample1.java ├── IntExample2.java ├── IntExample3.java ├── IntExample4.java ├── Java_Basic ├── Details.class ├── Details.java └── HelloWorld.java ├── Method Overloading └── Over.java ├── MultilineSentences.java ├── Pattern1.java ├── Pattern2.java ├── Pattern3.java ├── PrimitiveDataTypes ├── PrimitiveDataTypes.class └── PrimitiveDataTypes.java ├── PrintNumbers.java ├── README.md ├── ScaAddInt.java ├── ScaAddInt2.java ├── Scan1.java ├── Scan2.java ├── Scan3.java ├── ScanSubInt.java ├── ShortDeclaration.java ├── ShortHand.java ├── SwapValue1.java ├── SwapValue2.java ├── UnaryOpera.java ├── declareFloat.java └── declaringInt.java /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "java.debug.settings.onBuildFailureProceed": true 3 | } -------------------------------------------------------------------------------- /AbstMain.java: -------------------------------------------------------------------------------- 1 | /*Abstraction is a fundamental concept in object-oriented programming (OOP) that allows you to define classes that represent abstract concepts, hiding the implementation details and exposing only the necessary parts. In Java, abstraction is achieved using abstract classes and interfaces. 2 | 3 | Abstract Classes 4 | An abstract class is a class that cannot be instantiated and may contain abstract methods, which are methods without a body. Concrete subclasses must provide implementations for these abstract methods. */ 5 | 6 | 7 | abstract class Animal { 8 | // Abstract method (does not have a body) 9 | public abstract void makeSound(); 10 | 11 | // Regular method 12 | public void sleep() { 13 | System.out.println("The animal is sleeping."); 14 | } 15 | } 16 | 17 | class Dog extends Animal { 18 | // Providing implementation for the abstract method 19 | public void makeSound() { 20 | System.out.println("Woof"); 21 | } 22 | } 23 | 24 | public class AbstMain { 25 | public static void main(String[] args) { 26 | Dog myDog = new Dog(); 27 | myDog.makeSound(); // Outputs: Woof 28 | myDog.sleep(); // Outputs: The animal is sleeping. 29 | } 30 | } 31 | 32 | 33 | /*Sure! Let's break down the code step by step and explain it in simple terms. 34 | 35 | ### Abstract Class and Abstract Method 36 | 37 | **Abstract Class**: 38 | - `Animal` is an abstract class. This means it cannot be instantiated directly (you can't create an object of `Animal`). 39 | - An abstract class can have both abstract methods (methods without a body) and regular methods (methods with a body). 40 | 41 | **Abstract Method**: 42 | - `public abstract void makeSound();` is an abstract method. It does not have a body, meaning it doesn't provide an implementation. Any class that extends `Animal` must provide an implementation for this method. 43 | 44 | ### Concrete Class (Subclass) 45 | 46 | **Concrete Class**: 47 | - `Dog` is a class that extends `Animal`. This means `Dog` inherits from `Animal` and must implement all abstract methods in `Animal`. 48 | 49 | **Implementing the Abstract Method**: 50 | - `public void makeSound() { System.out.println("Woof"); }` provides the implementation for the `makeSound` method. When you call `makeSound` on a `Dog` object, it prints "Woof". 51 | 52 | ### Regular Method in Abstract Class 53 | 54 | - `public void sleep() { System.out.println("The animal is sleeping."); }` is a regular method in the `Animal` class. It has a body, and it prints "The animal is sleeping." when called. 55 | 56 | ### Main Method 57 | 58 | **Main Method**: 59 | - `public class AbstMain` is the main class where the program execution starts. 60 | - Inside the `main` method, a `Dog` object is created: `Dog myDog = new Dog();`. 61 | - The `makeSound` method is called on `myDog`, which outputs "Woof". 62 | - The `sleep` method is called on `myDog`, which outputs "The animal is sleeping." 63 | 64 | ### Simple Explanation 65 | 66 | 1. **Abstract Class and Method**: 67 | - You have a blueprint (`Animal`) that says every animal should be able to make a sound, but it doesn't specify what the sound is. This is like saying, "Every vehicle should be able to start," without specifying how. 68 | 69 | 2. **Concrete Class (Dog)**: 70 | - `Dog` is a specific type of animal. It knows exactly what sound it makes, so it provides the implementation for `makeSound` by saying "Woof". 71 | 72 | 3. **Regular Method (sleep)**: 73 | - `sleep` is a regular method that just prints "The animal is sleeping." It's like a general feature that all animals have. 74 | 75 | 4. **Main Method**: 76 | - You create a `Dog` object. 77 | - When you ask the dog to make a sound, it says "Woof". 78 | - When you tell the dog to sleep, it says "The animal is sleeping." 79 | 80 | This way, you can create different animals (like `Cat`, `Bird`, etc.) by extending the `Animal` class and providing their specific sounds while sharing common behaviors like sleeping. */ 81 | -------------------------------------------------------------------------------- /Add1.java: -------------------------------------------------------------------------------- 1 | public class Add1 { 2 | public static void main(String[] args) { 3 | Integer number1 = 80; // Autoboxing from int to Integer 4 | Integer number2 = 20; 5 | 6 | System.out.println( number1 + number2 ); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /ArithmeticOperators.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class ArithmeticOperators { 3 | public static void main(String[] args) { 4 | 5 | Scanner A0 = new Scanner (System.in); 6 | System.out.println("Give me 1st number: "); 7 | int a = A0.nextInt(); 8 | System.out.println("Give me 2nd number: "); 9 | int b = A0.nextInt(); 10 | 11 | System.out.println("Addition: " +(a+b)); 12 | System.out.println("Substraction: " +(a-b)); 13 | System.out.println("Multiplication: " +(a*b)); 14 | System.out.println("Division: " +(a/b)); 15 | System.out.println("Remainder: " +(a%b)); 16 | } 17 | } -------------------------------------------------------------------------------- /BooleanDeclaration.java: -------------------------------------------------------------------------------- 1 | public class BooleanDeclaration { 2 | public static void main(String[] args) { 3 | // Direct declaration and initialization 4 | boolean boolLiteralTrue = true; 5 | boolean boolLiteralFalse = false; 6 | 7 | // Using expressions and conditions 8 | boolean isGreater = 10 > 5; // true 9 | boolean isEqual = 10 == 10; // true 10 | 11 | // Using logical operators 12 | boolean andOperation = true && false; // false 13 | boolean orOperation = true || false; // true 14 | boolean notOperation = !true; // false 15 | 16 | // Using the Boolean wrapper class 17 | Boolean booleanObject = true; // Autoboxing 18 | boolean booleanFromObject = booleanObject; // Unboxing 19 | 20 | // Using String parsing 21 | boolean parsedBoolean = Boolean.parseBoolean("true"); 22 | 23 | // Print values 24 | System.out.println("boolLiteralTrue: " + boolLiteralTrue); 25 | System.out.println("boolLiteralFalse: " + boolLiteralFalse); 26 | System.out.println("isGreater: " + isGreater); 27 | System.out.println("isEqual: " + isEqual); 28 | System.out.println("andOperation: " + andOperation); 29 | System.out.println("orOperation: " + orOperation); 30 | System.out.println("notOperation: " + notOperation); 31 | System.out.println("booleanObject: " + booleanObject); 32 | System.out.println("booleanFromObject: " + booleanFromObject); 33 | System.out.println("parsedBoolean: " + parsedBoolean); 34 | } 35 | } 36 | 37 | 38 | /*This Java program demonstrates various ways to declare and use `boolean` variables. Here’s a detailed explanation of each part of the code: 39 | 40 | ### Code Explanation 41 | 42 | 1. **Class Declaration and Main Method:** 43 | ```java 44 | public class BooleanDeclaration { 45 | public static void main(String[] args) { 46 | ``` 47 | - The class `BooleanDeclaration` is defined. 48 | - The `main` method is the entry point of the program. 49 | 50 | 2. **Direct Declaration and Initialization:** 51 | ```java 52 | boolean boolLiteralTrue = true; 53 | boolean boolLiteralFalse = false; 54 | ``` 55 | - Two `boolean` variables `boolLiteralTrue` and `boolLiteralFalse` are declared and initialized with the literal values `true` and `false`, respectively. 56 | 57 | 3. **Using Expressions and Conditions:** 58 | ```java 59 | boolean isGreater = 10 > 5; // true 60 | boolean isEqual = 10 == 10; // true 61 | ``` 62 | - `boolean` variable `isGreater` is assigned the result of the expression `10 > 5`, which is `true`. 63 | - `boolean` variable `isEqual` is assigned the result of the expression `10 == 10`, which is also `true`. 64 | 65 | 4. **Using Logical Operators:** 66 | ```java 67 | boolean andOperation = true && false; // false 68 | boolean orOperation = true || false; // true 69 | boolean notOperation = !true; // false 70 | ``` 71 | - `boolean` variable `andOperation` is assigned the result of the logical AND operation `true && false`, which is `false`. 72 | - `boolean` variable `orOperation` is assigned the result of the logical OR operation `true || false`, which is `true`. 73 | - `boolean` variable `notOperation` is assigned the result of the logical NOT operation `!true`, which is `false`. 74 | 75 | 5. **Using the Boolean Wrapper Class:** 76 | ```java 77 | Boolean booleanObject = true; // Autoboxing 78 | boolean booleanFromObject = booleanObject; // Unboxing 79 | ``` 80 | - `Boolean` object `booleanObject` is initialized with a `boolean` value `true`. This process is known as autoboxing, where a primitive `boolean` is automatically converted to a `Boolean` object. 81 | - The `boolean` value is retrieved from the `Boolean` object using unboxing, where the `Boolean` object is automatically converted to a primitive `boolean`. 82 | 83 | 6. **Using String Parsing:** 84 | ```java 85 | boolean parsedBoolean = Boolean.parseBoolean("true"); 86 | ``` 87 | - `boolean` variable `parsedBoolean` is initialized by parsing the string `"true"` using `Boolean.parseBoolean()`, which converts the string to a `boolean`. 88 | 89 | 7. **Printing Values:** 90 | ```java 91 | System.out.println("boolLiteralTrue: " + boolLiteralTrue); 92 | System.out.println("boolLiteralFalse: " + boolLiteralFalse); 93 | System.out.println("isGreater: " + isGreater); 94 | System.out.println("isEqual: " + isEqual); 95 | System.out.println("andOperation: " + andOperation); 96 | System.out.println("orOperation: " + orOperation); 97 | System.out.println("notOperation: " + notOperation); 98 | System.out.println("booleanObject: " + booleanObject); 99 | System.out.println("booleanFromObject: " + booleanFromObject); 100 | System.out.println("parsedBoolean: " + parsedBoolean); 101 | ``` 102 | - Each of the `boolean` variables is printed to the console using `System.out.println`. 103 | 104 | ### Output Explanation 105 | When this program is run, it will print the following: 106 | 107 | ``` 108 | boolLiteralTrue: true 109 | boolLiteralFalse: false 110 | isGreater: true 111 | isEqual: true 112 | andOperation: false 113 | orOperation: true 114 | notOperation: false 115 | booleanObject: true 116 | booleanFromObject: true 117 | parsedBoolean: true 118 | ``` 119 | 120 | - **boolLiteralTrue:** Prints the value `true` assigned using the boolean literal. 121 | - **boolLiteralFalse:** Prints the value `false` assigned using the boolean literal. 122 | - **isGreater:** Prints the result of the comparison `10 > 5`, which is `true`. 123 | - **isEqual:** Prints the result of the comparison `10 == 10`, which is `true`. 124 | - **andOperation:** Prints the result of the logical AND operation `true && false`, which is `false`. 125 | - **orOperation:** Prints the result of the logical OR operation `true || false`, which is `true`. 126 | - **notOperation:** Prints the result of the logical NOT operation `!true`, which is `false`. 127 | - **booleanObject:** Prints the value `true` stored in the `Boolean` object due to autoboxing. 128 | - **booleanFromObject:** Prints the value `true` extracted from the `Boolean` object due to unboxing. 129 | - **parsedBoolean:** Prints the value `true` obtained from parsing the string `"true"`. 130 | 131 | This program effectively demonstrates the different methods to declare and work with `boolean` variables in Java. */ -------------------------------------------------------------------------------- /CharDeclaration.java: -------------------------------------------------------------------------------- 1 | public class CharDeclaration { 2 | public static void main(String[] args) { 3 | // Direct declaration and initialization with a character literal 4 | char charLiteral = 'A'; 5 | 6 | // Using Unicode values 7 | char unicodeChar = '\u0041'; // Unicode for 'A' 8 | 9 | // Using integer values (ASCII) 10 | char asciiChar = 65; // ASCII for 'A' 11 | 12 | // Using escape sequences 13 | char newLine = '\n'; 14 | char tab = '\t'; 15 | 16 | // Using the Character wrapper class 17 | Character charObject = 'A'; // Autoboxing 18 | char charFromObject = charObject; // Unboxing 19 | 20 | // Print values 21 | System.out.println("charLiteral: " + charLiteral); 22 | System.out.println("unicodeChar: " + unicodeChar); 23 | System.out.println("asciiChar: " + asciiChar); 24 | System.out.println("newLine (printed as a line break)"); 25 | System.out.println("tab: " + tab + " (printed as a tab space)"); 26 | System.out.println("charObject: " + charObject); 27 | System.out.println("charFromObject: " + charFromObject); 28 | } 29 | } 30 | 31 | /*This Java program demonstrates various ways to declare and use `char` variables. Here’s a detailed explanation of each part of the code: 32 | 33 | ### Code Explanation 34 | 35 | 1. **Class Declaration and Main Method:** 36 | ```java 37 | public class CharDeclaration { 38 | public static void main(String[] args) { 39 | ``` 40 | - The class `CharDeclaration` is defined. 41 | - The `main` method is the entry point of the program. 42 | 43 | 2. **Direct Declaration and Initialization with a Character Literal:** 44 | ```java 45 | char charLiteral = 'A'; 46 | ``` 47 | - A `char` variable `charLiteral` is declared and initialized with the character literal `'A'`. 48 | 49 | 3. **Using Unicode Values:** 50 | ```java 51 | char unicodeChar = '\u0041'; // Unicode for 'A' 52 | ``` 53 | - A `char` variable `unicodeChar` is declared and initialized with the Unicode value `\u0041`, which represents the character `'A'`. 54 | 55 | 4. **Using Integer Values (ASCII):** 56 | ```java 57 | char asciiChar = 65; // ASCII for 'A' 58 | ``` 59 | - A `char` variable `asciiChar` is declared and initialized with the ASCII value `65`, which represents the character `'A'`. 60 | 61 | 5. **Using Escape Sequences:** 62 | ```java 63 | char newLine = '\n'; 64 | char tab = '\t'; 65 | ``` 66 | - A `char` variable `newLine` is declared and initialized with the escape sequence `'\n'`, which represents a new line character. 67 | - A `char` variable `tab` is declared and initialized with the escape sequence `'\t'`, which represents a tab character. 68 | 69 | 6. **Using the Character Wrapper Class:** 70 | ```java 71 | Character charObject = 'A'; // Autoboxing 72 | char charFromObject = charObject; // Unboxing 73 | ``` 74 | - A `Character` object `charObject` is initialized with a `char` value `'A'`. This process is known as autoboxing, where a primitive `char` is automatically converted to a `Character` object. 75 | - The `char` value is retrieved from the `Character` object using unboxing, where the `Character` object is automatically converted to a primitive `char`. 76 | 77 | 7. **Printing Values:** 78 | ```java 79 | System.out.println("charLiteral: " + charLiteral); 80 | System.out.println("unicodeChar: " + unicodeChar); 81 | System.out.println("asciiChar: " + asciiChar); 82 | System.out.println("newLine (printed as a line break)"); 83 | System.out.println("tab: " + tab + " (printed as a tab space)"); 84 | System.out.println("charObject: " + charObject); 85 | System.out.println("charFromObject: " + charFromObject); 86 | ``` 87 | - Each of the `char` variables is printed to the console using `System.out.println`. 88 | 89 | ### Output Explanation 90 | When this program is run, it will print the following: 91 | 92 | ``` 93 | charLiteral: A 94 | unicodeChar: A 95 | asciiChar: A 96 | newLine (printed as a line break) 97 | tab: (printed as a tab space) 98 | charObject: A 99 | charFromObject: A 100 | ``` 101 | 102 | - **charLiteral:** Prints the character `'A'` assigned using a character literal. 103 | - **unicodeChar:** Prints the character `'A'` represented by the Unicode value `\u0041`. 104 | - **asciiChar:** Prints the character `'A'` represented by the ASCII value `65`. 105 | - **newLine:** Prints a new line (the actual output will have a line break). 106 | - **tab:** Prints a tab space (the actual output will have a tab space). 107 | - **charObject:** Prints the character `'A'` stored in the `Character` object due to autoboxing. 108 | - **charFromObject:** Prints the character `'A'` extracted from the `Character` object due to unboxing. 109 | 110 | This program effectively demonstrates the different methods to declare and work with `char` variables in Java. */ -------------------------------------------------------------------------------- /ClaMain.java: -------------------------------------------------------------------------------- 1 | /*A class in Java is a blueprint for creating objects. It defines a datatype by bundling data and methods that work on the data into a single unit. A class provides the structure and behavior that the objects created from it will have. 2 | 3 | Key Components of a Class 4 | Fields (Attributes/Variables): These are data members of the class that hold the state or properties of an object. 5 | Methods: These define the behavior or functionality of the class. 6 | Constructor: This is a special method used to initialize objects of the class. 7 | How to Construct a Class in Java 8 | To construct a class in Java, you need to: 9 | 10 | Define the class: Use the class keyword followed by the class name. 11 | Declare fields: Define the variables that represent the properties of the class. 12 | Define methods: Implement the functions that define the behavior of the class. 13 | Create a constructor: Define a constructor to initialize the object. 14 | Example 15 | Let's create a simple class Car with fields, methods, and a constructor. */ 16 | 17 | // Define the Car class 18 | class Car { 19 | // Fields (Variables) 20 | private String make; 21 | private String model; 22 | private int year; 23 | 24 | // Constructor to initialize the Car object 25 | public Car(String make, String model, int year) { 26 | this.make = make; 27 | this.model = model; 28 | this.year = year; 29 | } 30 | 31 | // Method to display the car details 32 | public void displayCarInfo() { 33 | System.out.println("Make: " + make + ", Model: " + model + ", Year: " + year); 34 | } 35 | 36 | // Method to start the car 37 | public void start() { 38 | System.out.println("The car is starting..."); 39 | } 40 | 41 | // Method to stop the car 42 | public void stop() { 43 | System.out.println("The car is stopping..."); 44 | } 45 | } 46 | 47 | // Main class to run the program 48 | public class ClaMain { 49 | public static void main(String[] args) { 50 | // Create a Car object 51 | Car myCar = new Car("Toyota", "Corolla", 2020); 52 | 53 | // Display car details 54 | myCar.displayCarInfo(); // Outputs: Make: Toyota, Model: Corolla, Year: 2020 55 | 56 | // Start the car 57 | myCar.start(); // Outputs: The car is starting... 58 | 59 | // Stop the car 60 | myCar.stop(); // Outputs: The car is stopping... 61 | } 62 | } 63 | 64 | /*Explanation 65 | Class Definition (Car): 66 | 67 | The class is defined using the class keyword followed by the class name (Car). 68 | The class contains three fields: make, model, and year. These fields represent the properties of a car. 69 | Constructor: 70 | 71 | The constructor public Car(String make, String model, int year) is used to initialize the fields when a new Car object is created. The constructor name is the same as the class name and it does not have a return type. 72 | Methods: 73 | 74 | displayCarInfo(): This method prints the car's make, model, and year. 75 | start(): This method prints a message indicating the car is starting. 76 | stop(): This method prints a message indicating the car is stopping. 77 | Main Class (Main): 78 | 79 | The Main class contains the main method, which is the entry point of the program. 80 | Inside the main method, a new Car object (myCar) is created with the make "Toyota", model "Corolla", and year 2020. 81 | The displayCarInfo(), start(), and stop() methods are called on the myCar object to demonstrate the functionality of the Car class. 82 | Summary 83 | A class is a blueprint for creating objects. 84 | A class in Java is constructed using the class keyword followed by the class name. 85 | It includes fields (to store data), methods (to perform operations), and constructors (to initialize objects). 86 | Objects are instances of classes and are created using the new keyword followed by a call to the constructor. */ 87 | 88 | /*Sure! Let's break down the code step by step and explain it in simple terms. 89 | 90 | ### Car Class 91 | 92 | The `Car` class is a blueprint for creating car objects. It defines the properties and behaviors of a car. 93 | 94 | 1. **Fields (Variables)**: 95 | - `private String make;` 96 | - `private String model;` 97 | - `private int year;` 98 | 99 | These are the properties of a car. They are private, which means they can't be accessed directly from outside the class. 100 | 101 | 2. **Constructor**: 102 | ```java 103 | public Car(String make, String model, int year) { 104 | this.make = make; 105 | this.model = model; 106 | this.year = year; 107 | } 108 | ``` 109 | The constructor is a special method that initializes the car's properties when a new `Car` object is created. It takes three parameters (`make`, `model`, and `year`) and assigns them to the corresponding fields. 110 | 111 | 3. **Methods**: 112 | - `displayCarInfo()`: This method prints out the car's details (make, model, and year). 113 | ```java 114 | public void displayCarInfo() { 115 | System.out.println("Make: " + make + ", Model: " + model + ", Year: " + year); 116 | } 117 | ``` 118 | - `start()`: This method prints a message indicating that the car is starting. 119 | ```java 120 | public void start() { 121 | System.out.println("The car is starting..."); 122 | } 123 | ``` 124 | - `stop()`: This method prints a message indicating that the car is stopping. 125 | ```java 126 | public void stop() { 127 | System.out.println("The car is stopping..."); 128 | } 129 | ``` 130 | 131 | ### ClaMain Class 132 | 133 | The `ClaMain` class contains the `main` method, which is the entry point of the program. 134 | 135 | 1. **Main Method**: 136 | ```java 137 | public static void main(String[] args) { 138 | // Create a Car object 139 | Car myCar = new Car("Toyota", "Corolla", 2020); 140 | 141 | // Display car details 142 | myCar.displayCarInfo(); // Outputs: Make: Toyota, Model: Corolla, Year: 2020 143 | 144 | // Start the car 145 | myCar.start(); // Outputs: The car is starting... 146 | 147 | // Stop the car 148 | myCar.stop(); // Outputs: The car is stopping... 149 | } 150 | ``` 151 | - **Create a Car object**: `Car myCar = new Car("Toyota", "Corolla", 2020);` 152 | This line creates a new `Car` object with the make "Toyota", model "Corolla", and year 2020. The `Car` constructor is called to initialize these values. 153 | 154 | - **Display car details**: `myCar.displayCarInfo();` 155 | This line calls the `displayCarInfo` method on the `myCar` object, which prints out the car's make, model, and year. 156 | 157 | - **Start the car**: `myCar.start();` 158 | This line calls the `start` method on the `myCar` object, which prints a message indicating that the car is starting. 159 | 160 | - **Stop the car**: `myCar.stop();` 161 | This line calls the `stop` method on the `myCar` object, which prints a message indicating that the car is stopping. 162 | 163 | ### Summary 164 | 165 | - **Class Definition**: The `Car` class defines the properties and behaviors of a car. 166 | - **Fields**: The properties of the car (make, model, year) are stored in private variables. 167 | - **Constructor**: Initializes the car's properties when a new `Car` object is created. 168 | - **Methods**: Define the behaviors of the car (displaying information, starting, stopping). 169 | - **Main Class**: The `ClaMain` class contains the `main` method, where a `Car` object is created and its methods are called to display its information, start, and stop it. 170 | 171 | This example shows how to define a class in Java, create an object from the class, and use its methods to perform actions. */ -------------------------------------------------------------------------------- /DoubleDeclaring.java: -------------------------------------------------------------------------------- 1 | public class DoubleDeclaring { 2 | public static void main(String[] args) { 3 | // Using double literal 4 | double doubleLiteral = 10.5; 5 | 6 | // Using type casting from a float value 7 | double doubleCast = (double) 10.5f; 8 | 9 | // Using the result of a calculation 10 | double doubleCalculation = 10.0 / 2; 11 | 12 | // Using the Double class 13 | Double doubleObject = 10.5; // Autoboxing 14 | double doubleFromObject = doubleObject; // Unboxing 15 | 16 | // Print values 17 | System.out.println("doubleLiteral: " + doubleLiteral); 18 | System.out.println("doubleCast: " + doubleCast); 19 | System.out.println("doubleCalculation: " + doubleCalculation); 20 | System.out.println("doubleObject: " + doubleObject); 21 | System.out.println("doubleFromObject: " + doubleFromObject); 22 | } 23 | } 24 | 25 | /* 26 | This Java program demonstrates different ways to declare and use `double` variables. Here’s a step-by-step explanation of the code: 27 | 28 | ### Code Explanation 29 | 30 | 1. **Class Declaration:** 31 | ```java 32 | public class DoubleDeclaration { 33 | public static void main(String[] args) { 34 | ``` 35 | - The class `DoubleDeclaration` is defined. 36 | - The `main` method is the entry point of the program. 37 | 38 | 2. **Using double literals:** 39 | ```java 40 | double doubleLiteral = 10.5; 41 | ``` 42 | - A `double` variable `doubleLiteral` is declared and initialized with a literal value `10.5`. 43 | 44 | 3. **Using type casting from a float value:** 45 | ```java 46 | double doubleCast = (double) 10.5f; 47 | ``` 48 | - A `float` literal `10.5f` is explicitly cast to `double` using `(double)` before assignment to the `double` variable `doubleCast`. 49 | 50 | 4. **Using the result of a calculation:** 51 | ```java 52 | double doubleCalculation = 10.0 / 2; 53 | ``` 54 | - A calculation `10.0 / 2` is performed. Since `10.0` is a `double` literal, the result is also a `double`. 55 | - The result of the calculation is assigned to `doubleCalculation`. 56 | 57 | 5. **Using the `Double` class:** 58 | ```java 59 | Double doubleObject = 10.5; // Autoboxing 60 | double doubleFromObject = doubleObject; // Unboxing 61 | ``` 62 | - A `Double` object `doubleObject` is initialized with a `double` value `10.5`. This is known as autoboxing, where a primitive `double` is automatically converted to a `Double` object. 63 | - The `double` value is retrieved from the `Double` object using unboxing, where the `Double` object is automatically converted to a primitive `double`. 64 | 65 | 6. **Printing values:** 66 | ```java 67 | System.out.println("doubleLiteral: " + doubleLiteral); 68 | System.out.println("doubleCast: " + doubleCast); 69 | System.out.println("doubleCalculation: " + doubleCalculation); 70 | System.out.println("doubleObject: " + doubleObject); 71 | System.out.println("doubleFromObject: " + doubleFromObject); 72 | ``` 73 | - Each of the `double` variables is printed to the console using `System.out.println`. 74 | 75 | ### Output Explanation 76 | When this program is run, it will print the following: 77 | 78 | ``` 79 | doubleLiteral: 10.5 80 | doubleCast: 10.5 81 | doubleCalculation: 5.0 82 | doubleObject: 10.5 83 | doubleFromObject: 10.5 84 | ``` 85 | 86 | - **doubleLiteral:** Prints the value `10.5` assigned using the double literal. 87 | - **doubleCast:** Prints the value `10.5` which was type cast from a `float` literal. 88 | - **doubleCalculation:** Prints the result of the division calculation, which is `5.0`. 89 | - **doubleObject:** Prints the value `10.5` stored in the `Double` object due to autoboxing. 90 | - **doubleFromObject:** Prints the value `10.5` extracted from the `Double` object due to unboxing. 91 | 92 | This program effectively demonstrates the different methods to declare and work with `double` variables in Java. 93 | */ 94 | -------------------------------------------------------------------------------- /EncapMain.java: -------------------------------------------------------------------------------- 1 | /*Encapsulation in Java is a mechanism of wrapping the data (variables) and the code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class are hidden from other classes and can be accessed only through the methods of their current class. This is done to protect the data from being accessed directly and to enforce a controlled and secure way of accessing it. 2 | 3 | Key Points of Encapsulation: 4 | Data Hiding: The data (variables) is hidden from other classes and can only be accessed through the methods (getters and setters) of the class in which it is defined. 5 | Controlled Access: Methods can be used to control how the data is accessed and modified. 6 | Improves Maintainability: By keeping the data and methods together, it becomes easier to manage and maintain the code. */ 7 | 8 | class Person { 9 | // Private variables - encapsulated data 10 | private String name; 11 | private int age; 12 | 13 | // Constructor to initialize Person object 14 | public Person(String name, int age) { 15 | this.name = name; 16 | this.age = age; 17 | } 18 | 19 | // Getter method for name 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | // Setter method for name 25 | public void setName(String name) { 26 | this.name = name; 27 | } 28 | 29 | // Getter method for age 30 | public int getAge() { 31 | return age; 32 | } 33 | 34 | // Setter method for age 35 | public void setAge(int age) { 36 | if (age > 0) { // Adding validation to ensure age is positive 37 | this.age = age; 38 | } else { 39 | System.out.println("Age must be positive."); 40 | } 41 | } 42 | 43 | // Method to display person details 44 | public void displayPersonInfo() { 45 | System.out.println("Name: " + name + ", Age: " + age); 46 | } 47 | } 48 | 49 | public class EncapMain { 50 | public static void main(String[] args) { 51 | // Creating a new Person object 52 | Person person = new Person("Alice", 30); 53 | 54 | // Accessing and modifying the data using methods 55 | person.displayPersonInfo(); // Outputs: Name: Alice, Age: 30 56 | 57 | person.setName("Bob"); 58 | person.setAge(35); 59 | 60 | person.displayPersonInfo(); // Outputs: Name: Bob, Age: 35 61 | 62 | // Attempting to set an invalid age 63 | person.setAge(-5); // Outputs: Age must be positive. 64 | } 65 | } 66 | 67 | /*Sure! Let's break down the code step by step and explain it in simple terms. 68 | 69 | ### Encapsulation in the `Person` Class 70 | 71 | **Encapsulation** is about bundling the data (variables) and methods (functions) that operate on the data into a single unit called a class. It also involves hiding the internal state of the object from the outside world and providing controlled access through methods. 72 | 73 | ### The `Person` Class 74 | 75 | 1. **Private Variables**: 76 | - `private String name;` and `private int age;` are private variables. This means they cannot be accessed directly from outside the `Person` class. 77 | 78 | 2. **Constructor**: 79 | - `public Person(String name, int age)` is a constructor. It is used to create objects of the `Person` class and initialize the variables `name` and `age`. 80 | 81 | 3. **Getter Methods**: 82 | - `public String getName()` and `public int getAge()` are getter methods. They allow other classes to read the values of `name` and `age`. 83 | 84 | 4. **Setter Methods**: 85 | - `public void setName(String name)` and `public void setAge(int age)` are setter methods. They allow other classes to modify the values of `name` and `age`. 86 | - The `setAge` method includes a validation check to ensure the age is positive. If the age is not positive, it prints an error message. 87 | 88 | 5. **Display Method**: 89 | - `public void displayPersonInfo()` is a method that prints the `name` and `age` of the person. 90 | 91 | ### The `EncapMain` Class 92 | 93 | This class contains the `main` method, which is the entry point of the program. 94 | 95 | 1. **Creating a `Person` Object**: 96 | - `Person person = new Person("Alice", 30);` creates a new `Person` object with the name "Alice" and age 30. 97 | 98 | 2. **Displaying Initial Information**: 99 | - `person.displayPersonInfo();` calls the method to display the person's details. It outputs: `Name: Alice, Age: 30`. 100 | 101 | 3. **Modifying the Data**: 102 | - `person.setName("Bob");` changes the name to "Bob". 103 | - `person.setAge(35);` changes the age to 35. 104 | 105 | 4. **Displaying Modified Information**: 106 | - `person.displayPersonInfo();` again calls the method to display the updated details. It outputs: `Name: Bob, Age: 35`. 107 | 108 | 5. **Attempting to Set an Invalid Age**: 109 | - `person.setAge(-5);` tries to set the age to -5. Since the age is not positive, the `setAge` method prints: `Age must be positive.` 110 | 111 | ### Simple Explanation 112 | 113 | 1. **Encapsulation**: 114 | - The `Person` class encapsulates the data (name and age) and provides methods to access and modify this data. The data is hidden from direct access to protect it and ensure it's used correctly. 115 | 116 | 2. **Constructor**: 117 | - The constructor initializes a new `Person` object with a name and age. 118 | 119 | 3. **Getters and Setters**: 120 | - Getter methods (`getName`, `getAge`) are used to read the data. 121 | - Setter methods (`setName`, `setAge`) are used to modify the data. The `setAge` method includes a check to ensure the age is positive. 122 | 123 | 4. **Display Method**: 124 | - The `displayPersonInfo` method prints the person's details. 125 | 126 | 5. **Main Method**: 127 | - In the `main` method, a `Person` object is created, its details are displayed, the data is modified, and the updated details are displayed again. An attempt to set an invalid age shows how the validation in the setter method works. 128 | 129 | This example shows how encapsulation helps protect the data and ensures it's accessed and modified in a controlled and secure way. */ 130 | 131 | -------------------------------------------------------------------------------- /FaceMain.java: -------------------------------------------------------------------------------- 1 | interface Animal { 2 | void makeSound(); // Interface method (does not have a body) 3 | void sleep(); // Interface method (does not have a body) 4 | } 5 | 6 | class Dog implements Animal { 7 | // Providing implementation for the interface methods 8 | public void makeSound() { 9 | System.out.println("Woof"); 10 | } 11 | 12 | public void sleep() { 13 | System.out.println("The dog is sleeping."); 14 | } 15 | } 16 | 17 | public class FaceMain { 18 | public static void main(String[] args) { 19 | Dog myDog = new Dog(); 20 | myDog.makeSound(); // Outputs: Woof 21 | myDog.sleep(); // Outputs: The dog is sleeping. 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /FloatMult.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class FloatMult { 3 | public static void main(String[] args) { 4 | 5 | Scanner floMult = new Scanner (System.in); 6 | System.out.println("Enter Your Two Number Separated with Commas: "); 7 | float a = floMult.nextInt(); 8 | float b = floMult.nextInt(); 9 | float c = (a*b); 10 | 11 | System.out.println("Your Answer is: " +c); 12 | 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /IntExample0.java: -------------------------------------------------------------------------------- 1 | public class IntExample0 { 2 | public static void main(String[] args) { 3 | 4 | 5 | System.out.println("Print My Number is 89"); 6 | System.out.println("Print My Number is 96"); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /IntExample1.java: -------------------------------------------------------------------------------- 1 | //Declare Int Example 1 2 | public class IntExample1 { 3 | public static void main(String[] args) { 4 | int number1 = 10; 5 | int number2 = 20; 6 | 7 | System.out.println("Number 1: " + number1); 8 | System.out.println("Number 2: " + number2); 9 | } 10 | } 11 | 12 | 13 | /* Sure, let's go through the code line by line to understand what each part does: 14 | 15 | ```java 16 | public class Example1 { 17 | ``` 18 | - This line declares a public class named `Example1`. In Java, every application must have at least one class definition. 19 | 20 | ```java 21 | public static void main(String[] args) { 22 | ``` 23 | - This line declares the `main` method. The `main` method is the entry point for any Java application. 24 | - `public`: This is an access modifier that makes the `main` method accessible from outside the class. 25 | - `static`: This keyword means that the method belongs to the class, rather than an instance of the class. It can be called without creating an instance of the class. 26 | - `void`: This means that the method does not return any value. 27 | - `String[] args`: This is an array of `String` objects, which can store command-line arguments passed to the program. 28 | 29 | ```java 30 | int number1 = 10; 31 | ``` 32 | - This line declares an integer variable named `number1` and initializes it with the value `10`. 33 | 34 | ```java 35 | int number2 = 20; 36 | ``` 37 | - This line declares another integer variable named `number2` and initializes it with the value `20`. 38 | 39 | ```java 40 | System.out.println("Number 1: " + number1); 41 | ``` 42 | - This line prints the string `"Number 1: "` followed by the value of `number1` to the console. The `+` operator is used to concatenate the string and the value of the variable. 43 | 44 | ```java 45 | System.out.println("Number 2: " + number2); 46 | ``` 47 | - This line prints the string `"Number 2: "` followed by the value of `number2` to the console. 48 | 49 | ```java 50 | } 51 | } 52 | ``` 53 | - These lines close the `main` method and the `Example1` class definition, respectively. 54 | 55 | In summary, the program declares two integer variables, initializes them with values, and then prints these values to the console. When executed, the output will be: 56 | 57 | ``` 58 | Number 1: 10 59 | Number 2: 20 60 | ``` */ -------------------------------------------------------------------------------- /IntExample2.java: -------------------------------------------------------------------------------- 1 | public class IntExample2 { 2 | public static void main(String[] args) { 3 | int number1; 4 | int number2; 5 | 6 | number1 = 30; 7 | number2 = 40; 8 | 9 | System.out.println("Number 1: " + number1); 10 | System.out.println("Number 2: " + number2); 11 | } 12 | } 13 | 14 | 15 | /*Certainly! Let's go through the `IntExample2` class code line by line to understand what it does: 16 | 17 | ```java 18 | public class IntExample2 { 19 | ``` 20 | - This line declares a public class named `IntExample2`. In Java, a class is a blueprint for objects that encapsulates data and methods. The `public` keyword means this class can be accessed from other classes. 21 | 22 | ```java 23 | public static void main(String[] args) { 24 | ``` 25 | - This line declares the `main` method, which is the entry point of any Java application. The `public` keyword means it can be accessed from outside the class. `static` means this method can be called without creating an instance of the class. `void` means this method does not return any value. `String[] args` is an array of strings that stores command-line arguments passed to the program. 26 | 27 | ```java 28 | int number1; 29 | int number2; 30 | ``` 31 | - These lines declare two integer variables, `number1` and `number2`. At this point, they are declared but not yet initialized. 32 | 33 | ```java 34 | number1 = 30; 35 | ``` 36 | - This line assigns the value `30` to the variable `number1`. 37 | 38 | ```java 39 | number2 = 40; 40 | ``` 41 | - This line assigns the value `40` to the variable `number2`. 42 | 43 | ```java 44 | System.out.println("Number 1: " + number1); 45 | ``` 46 | - This line prints the value of `number1` to the console. The output will be the string "Number 1: " followed by the value of `number1`, which is `30`. 47 | 48 | ```java 49 | System.out.println("Number 2: " + number2); 50 | ``` 51 | - This line prints the value of `number2` to the console. The output will be the string "Number 2: " followed by the value of `number2`, which is `40`. 52 | 53 | ```java 54 | } 55 | ``` 56 | - This line closes the `main` method. 57 | 58 | ```java 59 | } 60 | ``` 61 | - This line closes the `IntExample2` class. 62 | 63 | ### Summary 64 | When you run this program, it will declare two integer variables, `number1` and `number2`, assign the values `30` and `40` to them respectively, and then print these values to the console. The output of the program will be: 65 | 66 | ``` 67 | Number 1: 30 68 | Number 2: 40 69 | ``` 70 | 71 | This simple program demonstrates how to declare variables, assign values to them, and print those values using the `System.out.println` method in Java. */ -------------------------------------------------------------------------------- /IntExample3.java: -------------------------------------------------------------------------------- 1 | public class IntExample3 { 2 | public static void main(String[] args) { 3 | int number1 = 50, number2 = 60, number3 = 70; 4 | 5 | System.out.println("Number 1: " + number1); 6 | System.out.println("Number 2: " + number2); 7 | System.out.println("Number 3: " + number3); 8 | } 9 | } 10 | 11 | /* Certainly! Let's go through the `Example3` class code line by line to understand what it does: 12 | 13 | ```java 14 | public class Example3 { 15 | ``` 16 | - This line declares a public class named `Example3`. In Java, a class is a blueprint for objects that encapsulates data and methods. The `public` keyword means this class can be accessed from other classes. 17 | 18 | ```java 19 | public static void main(String[] args) { 20 | ``` 21 | - This line declares the `main` method, which is the entry point of any Java application. The `public` keyword means it can be accessed from outside the class. `static` means this method can be called without creating an instance of the class. `void` means this method does not return any value. `String[] args` is an array of strings that stores command-line arguments passed to the program. 22 | 23 | ```java 24 | int number1 = 50, number2 = 60, number3 = 70; 25 | ``` 26 | - This line declares and initializes three integer variables, `number1`, `number2`, and `number3`, in a single statement. `number1` is assigned the value `50`, `number2` is assigned the value `60`, and `number3` is assigned the value `70`. 27 | 28 | ```java 29 | System.out.println("Number 1: " + number1); 30 | ``` 31 | - This line prints the value of `number1` to the console. The output will be the string "Number 1: " followed by the value of `number1`, which is `50`. 32 | 33 | ```java 34 | System.out.println("Number 2: " + number2); 35 | ``` 36 | - This line prints the value of `number2` to the console. The output will be the string "Number 2: " followed by the value of `number2`, which is `60`. 37 | 38 | ```java 39 | System.out.println("Number 3: " + number3); 40 | ``` 41 | - This line prints the value of `number3` to the console. The output will be the string "Number 3: " followed by the value of `number3`, which is `70`. 42 | 43 | ```java 44 | } 45 | ``` 46 | - This line closes the `main` method. 47 | 48 | ```java 49 | } 50 | ``` 51 | - This line closes the `Example3` class. 52 | 53 | ### Summary 54 | When you run this program, it will declare and initialize three integer variables, `number1`, `number2`, and `number3`, with the values `50`, `60`, and `70`, respectively. Then it will print these values to the console. The output of the program will be: 55 | 56 | ``` 57 | Number 1: 50 58 | Number 2: 60 59 | Number 3: 70 60 | ``` 61 | 62 | This simple program demonstrates how to declare and initialize multiple variables in a single statement and how to print their values using the `System.out.println` method in Java. */ -------------------------------------------------------------------------------- /IntExample4.java: -------------------------------------------------------------------------------- 1 | public class IntExample4 { 2 | public static void main(String[] args) { 3 | Integer number1 = 80; // Autoboxing from int to Integer 4 | Integer number2 = new Integer (90); // Explicitly creating an Integer object 5 | 6 | System.out.println("Number 1: " + number1); 7 | System.out.println("Number 2: " + number2); 8 | } 9 | } 10 | 11 | 12 | /*Certainly! Let's go through the `Example4` class code line by line to understand what it does: 13 | 14 | ```java 15 | public class Example4 { 16 | ``` 17 | - This line declares a public class named `Example4`. In Java, a class is a blueprint for objects that encapsulates data and methods. The `public` keyword means this class can be accessed from other classes. 18 | 19 | ```java 20 | public static void main(String[] args) { 21 | ``` 22 | - This line declares the `main` method, which is the entry point of any Java application. The `public` keyword means it can be accessed from outside the class. `static` means this method can be called without creating an instance of the class. `void` means this method does not return any value. `String[] args` is an array of strings that stores command-line arguments passed to the program. 23 | 24 | ```java 25 | Integer number1 = 80; // Autoboxing from int to Integer 26 | ``` 27 | - This line declares an `Integer` object named `number1` and initializes it with the value `80`. In this context, autoboxing is taking place, where the primitive `int` value `80` is automatically converted into an `Integer` object. 28 | 29 | ```java 30 | Integer number2 = new Integer(90); // Explicitly creating an Integer object 31 | ``` 32 | - This line declares another `Integer` object named `number2` and initializes it with the value `90` by explicitly creating a new `Integer` object using the `new` keyword. Note that creating `Integer` objects using the `new` keyword is generally discouraged in modern Java because it is less efficient than using autoboxing. 33 | 34 | ```java 35 | System.out.println("Number 1: " + number1); 36 | ``` 37 | - This line prints the value of `number1` to the console. The output will be the string "Number 1: " followed by the value of `number1`, which is `80`. 38 | 39 | ```java 40 | System.out.println("Number 2: " + number2); 41 | ``` 42 | - This line prints the value of `number2` to the console. The output will be the string "Number 2: " followed by the value of `number2`, which is `90`. 43 | 44 | ```java 45 | } 46 | ``` 47 | - This line closes the `main` method. 48 | 49 | ```java 50 | } 51 | ``` 52 | - This line closes the `Example4` class. 53 | 54 | ### Summary 55 | When you run this program, it will declare two `Integer` objects, `number1` and `number2`, with the values `80` and `90` respectively. `number1` is created using autoboxing, while `number2` is created explicitly using the `new` keyword. Then, it will print these values to the console. The output of the program will be: 56 | 57 | ``` 58 | Number 1: 80 59 | Number 2: 90 60 | ``` 61 | 62 | This example demonstrates the use of autoboxing and the explicit creation of `Integer` objects in Java, as well as printing their values using the `System.out.println` method. */ 63 | 64 | -------------------------------------------------------------------------------- /Java_Basic/Details.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PtRahul35/Java-Code/349affd90b762db8ed9303120a7115e793f8bcf1/Java_Basic/Details.class -------------------------------------------------------------------------------- /Java_Basic/Details.java: -------------------------------------------------------------------------------- 1 | //Program to print your details 2 | 3 | public class Details { 4 | 5 | public static void main (String [] args) { 6 | 7 | System.out.println("Name: Rahul Kumar"); 8 | System.out.println("Class: "); 9 | System.out.println("Section: "); 10 | System.out.println("Roll No: "); 11 | System.out.println("Date of Birth: "); 12 | } 13 | } -------------------------------------------------------------------------------- /Java_Basic/HelloWorld.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | public class HelloWorld { 4 | public static void main(String [] args ) { 5 | System.out.println("Hello, World!"); 6 | } 7 | } 8 | 9 | 10 | /* Here's a simple explanation of the "Hello World" Java program: 11 | 12 | ```java 13 | public class HelloWorld { 14 | public static void main(String[] args) { 15 | System.out.println("Hello, World!"); 16 | } 17 | } 18 | ``` 19 | 20 | ### Explanation 21 | 22 | 1. **public class HelloWorld {**: 23 | - This line defines a public class named `HelloWorld`. 24 | - A class is a blueprint for creating objects, and it contains methods (functions) and attributes (variables). 25 | 26 | 2. **public static void main(String[] args) {**: 27 | - This line defines the `main` method. 28 | - The `main` method is the entry point of any Java application. When you run the program, the code inside this method is executed first. 29 | - `public` means the method is accessible from anywhere. 30 | - `static` means this method belongs to the class itself, not to any specific instance of the class. 31 | - `void` means the method does not return any value. 32 | - `String[] args` is an array of strings that stores command-line arguments. This parameter is not used in this simple program, but it's part of the method signature. 33 | 34 | 3. **System.out.println("Hello, World!");**: 35 | - This line prints the text `Hello, World!` to the console. 36 | - `System` is a built-in class that provides access to the system. 37 | - `out` is a static member of the `System` class, which represents the standard output stream (usually the console). 38 | - `println` is a method of the `out` object, which prints the given string to the console followed by a new line. 39 | 40 | 4. **}**: 41 | - These closing braces mark the end of the `main` method and the `HelloWorld` class, respectively. 42 | 43 | ### Summary 44 | 45 | When you run this program, it executes the `main` method, which prints `Hello, World!` to the console. This is often the first program written by people learning a new programming language as it demonstrates the basic structure of a program and how to output text. */ -------------------------------------------------------------------------------- /Method Overloading/Over.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PtRahul35/Java-Code/349affd90b762db8ed9303120a7115e793f8bcf1/Method Overloading/Over.java -------------------------------------------------------------------------------- /MultilineSentences.java: -------------------------------------------------------------------------------- 1 | 2 | public class MultilineSentences { 3 | 4 | public static void main (String[]args) 5 | 6 | { 7 | System.out.println ("Hello World"); 8 | System.out.println ("I'm Learning Java"); 9 | System.out.println ("With Rahul Sir"); 10 | 11 | } 12 | } 13 | 14 | 15 | /*Let's break down the code step by step: 16 | 17 | 1. **Class Definition:** 18 | ```java 19 | public class MultilineSentences { 20 | ``` 21 | This line defines a public class named `MultilineSentences`. A class is a blueprint for creating objects and contains methods (functions) and variables. 22 | 23 | 2. **Main Method:** 24 | ```java 25 | public static void main(String[] args) { 26 | ``` 27 | This line defines the `main` method. The `main` method is the entry point of any Java program. When you run a Java application, the JVM (Java Virtual Machine) calls the `main` method. 28 | 29 | 3. **Opening Curly Brace:** 30 | ```java 31 | { 32 | ``` 33 | This opening brace marks the beginning of the `main` method's body. 34 | 35 | 4. **Printing to the Console:** 36 | ```java 37 | System.out.println("Hello World"); 38 | System.out.println("I'm Learning Java"); 39 | System.out.println("With Rahul Sir"); 40 | ``` 41 | These three lines print messages to the console. 42 | - `System.out.println("Hello World");` prints "Hello World" and moves the cursor to the next line. 43 | - `System.out.println("I'm Learning Java");` prints "I'm Learning Java" and moves the cursor to the next line. 44 | - `System.out.println("With Rahul Sir");` prints "With Rahul Sir" and moves the cursor to the next line. 45 | 46 | 5. **Closing Curly Brace:** 47 | ```java 48 | } 49 | ``` 50 | This closing brace marks the end of the `main` method's body. 51 | 52 | 6. **Another Closing Curly Brace:** 53 | ```java 54 | } 55 | ``` 56 | This closing brace marks the end of the `MultilineSentences` class. 57 | 58 | ### Summary 59 | When you run this Java program, it will print the following three lines to the console: 60 | ``` 61 | Hello World 62 | I'm Learning Java 63 | With Rahul Sir 64 | ``` */ -------------------------------------------------------------------------------- /Pattern1.java: -------------------------------------------------------------------------------- 1 | public class Pattern1 { 2 | 3 | public static void main(String[] args) { 4 | System.out.println("*"); 5 | System.out.println("**"); 6 | System.out.println("***"); 7 | System.out.println("****"); 8 | System.out.println("*****"); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Pattern2.java: -------------------------------------------------------------------------------- 1 | public class Pattern2 { 2 | 3 | public static void main(String[] args) { 4 | System.out.println(" *"); 5 | System.out.println(" **"); 6 | System.out.println(" ***"); 7 | System.out.println(" ****"); 8 | System.out.println(" *****"); 9 | } 10 | } -------------------------------------------------------------------------------- /Pattern3.java: -------------------------------------------------------------------------------- 1 | public class Pattern3 { 2 | 3 | public static void main(String[] args) { 4 | System.out.println("******"); 5 | System.out.println(" ****"); 6 | System.out.println(" ***"); 7 | System.out.println(" ****"); 8 | System.out.println("******"); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /PrimitiveDataTypes/PrimitiveDataTypes.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PtRahul35/Java-Code/349affd90b762db8ed9303120a7115e793f8bcf1/PrimitiveDataTypes/PrimitiveDataTypes.class -------------------------------------------------------------------------------- /PrimitiveDataTypes/PrimitiveDataTypes.java: -------------------------------------------------------------------------------- 1 | public class PrimitiveDataTypes { 2 | 3 | public static void main (String [] args) { 4 | 5 | byte bValue = 1; 6 | short Svalue = 10; 7 | int IValue = 256; 8 | long LValue = 100000L; 9 | float FValue = 4.5f; 10 | double DValue = 99.99; 11 | char cValue = 'A'; 12 | boolean BValue = true; 13 | 14 | System.out.println("Value of Byte Variable: " +bValue); 15 | System.out.println("Value of Short Variable: "+ Svalue); 16 | System.out.println("Value of Int Variable: "+IValue); 17 | System.out.println("Value of Long Variable: "+LValue); 18 | System.out.println("Value of Float Variable: "+FValue); 19 | System.out.println("Value of Double Variable: "+DValue); 20 | System.out.println("Value of Char Variable: "+cValue); 21 | System.out.println("Value of Boolean Variable: "+BValue); 22 | 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /PrintNumbers.java: -------------------------------------------------------------------------------- 1 | // How to print number 35, 70 , 140, 2800 2 | 3 | public class PrintNumbers { 4 | 5 | public static void main(String[] args) { 6 | System.out.println(35); 7 | System.out.println(70); 8 | System.out.println(140); 9 | System.out.println(2800); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Introduction 2 | Welcome to the Java Code for Students repository! 3 | This repository contains a collection of Java programs 4 | designed to help students learn and practice various concepts of Java programming. 5 | Whether you are a beginner or looking to enhance your Java skills, this repository 6 | provides a range of examples and exercises to support your learning journey. 7 | 8 | Repository Structure 9 | The repository is organized into directories based on different topics and levels of difficulty. 10 | Here is an overview of the structure: 11 | 12 | Java-Code-for-Students/ 13 | ├── Basic-Concepts/ 14 | │ ├── HelloWorld.java 15 | │ ├── Variables.java 16 | │ ├── DataTypes.java 17 | │ ├── Operators.java 18 | │ ├── ControlStatements.java 19 | ├── Object-Oriented-Programming/ 20 | │ ├── ClassesAndObjects.java 21 | │ ├── Inheritance.java 22 | │ ├── Polymorphism.java 23 | │ ├── Encapsulation.java 24 | │ ├── Abstraction.java 25 | ├── Data-Structures/ 26 | │ ├── Arrays.java 27 | │ ├── LinkedList.java 28 | │ ├── Stack.java 29 | │ ├── Queue.java 30 | │ ├── HashMap.java 31 | ├── Algorithms/ 32 | │ ├── SortingAlgorithms.java 33 | │ ├── SearchAlgorithms.java 34 | │ ├── Recursion.java 35 | ├── Advanced-Topics/ 36 | │ ├── Multithreading.java 37 | │ ├── FileIO.java 38 | │ ├── Networking.java 39 | 40 | 41 | Getting Started 42 | To get started with the Java programs in this repository, follow these steps: 43 | 44 | 1. Clone the Repository: Clone the repository to your local machine using the following command: 45 | git clone https://github.com/your-username/Java-Code-for-Students.git 46 | 47 | 2. Navigate to the Directory: Change to the directory where you cloned the repository 48 | cd Java-Code-for-Students 49 | 50 | 4. Compile and Run: Navigate to the specific directory of the program you want to run. Compile and execute the Java program using the following commands: 51 | javac ProgramName.java 52 | java ProgramName 53 | 54 | Prerequisites 55 | Ensure you have the following software installed on your machine: 56 | 57 | 1. Java Development Kit (JDK) 58 | 2. Git 59 | 60 | Issues 61 | If you encounter any issues or have questions, feel free to open an issue in the Issues tab of this repository. 62 | 63 | License 64 | This repository is licensed under the KRIYASIDHI Pvt. Ltd. License. See the LICENSE file for more information. 65 | 66 | Acknowledgements 67 | Special thanks to all the contributors who have helped in creating and maintaining this repository. 68 | 69 | Contact 70 | For any queries, please contact us at rahulkr3cs@gmail.com 71 | 72 | Facebook 73 | [facebook.com/profile.php?id=100076261449247](https://www.facebook.com/people/Pt_classes/100076261449247/) 74 | 75 | Instagram 76 | [instagram.com/ptsclasses](https://www.instagram.com/ptsclasses) 77 | 78 | X 79 | [x.com/ptsclasses](https://x.com/ptsclasses) 80 | 81 | Youtube: 82 | www.youtube.com/@rahulKr3cs 83 | 84 | Happy Coding! 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /ScaAddInt.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class ScaAddInt { 5 | 6 | public static void main(String[] args) { 7 | 8 | Scanner input = new Scanner(System.in); 9 | System.out.print("Enter Your two number: "); 10 | int a = input.nextInt(); 11 | System.out.println( "Your Number is " +a ); 12 | } 13 | 14 | } 15 | 16 | /*Certainly! Let's go through this code step by step: 17 | 18 | 1. **Importing Scanner Class**: 19 | ```java 20 | import java.util.Scanner; 21 | ``` 22 | This line imports the `Scanner` class from the `java.util` package, which is used to get user input. 23 | 24 | 2. **Class Declaration**: 25 | ```java 26 | public class ScaAddInt { 27 | ``` 28 | This line declares a public class named `ScaAddInt`. 29 | 30 | 3. **Main Method**: 31 | ```java 32 | public static void main(String[] args) { 33 | ``` 34 | This is the main method, which is the entry point of the program. The program starts executing from here. 35 | 36 | 4. **Creating Scanner Object**: 37 | ```java 38 | Scanner input = new Scanner(System.in); 39 | ``` 40 | This line creates a `Scanner` object named `input` that reads from the standard input (keyboard). 41 | 42 | 5. **Prompting and Reading User's Number**: 43 | ```java 44 | System.out.print("Enter Your two number: "); 45 | int a = input.nextInt(); 46 | ``` 47 | - `System.out.print("Enter Your two number: ");` displays the prompt "Enter Your two number: " without a newline. 48 | - `int a = input.nextInt();` reads an integer input by the user and stores it in the variable `a`. 49 | 50 | 6. **Displaying User's Number**: 51 | ```java 52 | System.out.println("Your Number is " + a); 53 | ``` 54 | - `System.out.println("Your Number is " + a);` displays "Your Number is " followed by the integer value stored in the variable `a`. 55 | 56 | ### Summary: 57 | This program prompts the user to enter a number (despite the prompt suggesting two numbers, the code only handles one). It then reads this number and displays it back to the user. There seems to be a mistake in the prompt message, as it suggests the user should enter two numbers, but the program only reads and displays one number. */ 58 | -------------------------------------------------------------------------------- /ScaAddInt2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class ScaAddInt2 { 4 | 5 | public static void main(String[] args) { 6 | 7 | try (Scanner input = new Scanner(System.in)) { 8 | System.out.print("Enter your two numbers separated by a space: "); 9 | // Read two integers from the input 10 | int a = input.nextInt(); 11 | int b = input.nextInt(); 12 | // Display the sum of the two numbers 13 | System.out.println("The sum of the two numbers is: " + (a + b)); 14 | // Close the scanner 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Scan1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Scan1 { 4 | 5 | public static void main(String[] args) { 6 | 7 | Scanner input = new Scanner (System.in); 8 | System.out.print("Please Enter Your Name: "); 9 | String name = input.nextLine(); 10 | System.out.println("Good Morning " + name); 11 | System.out.print(name + ", tell me your age"); 12 | int age = input.nextInt(); 13 | System.out.print("Your age is " +age); 14 | } 15 | } 16 | 17 | 18 | /*Sure! Here's an explanation of your Java code in simple words: 19 | 20 | 1. **Importing Scanner:** The line `import java.util.Scanner;` tells Java to use the Scanner class, which is a part of the Java library used for getting input from the user. 21 | 22 | 2. **Class Definition:** `public class Scan1 { ... }` defines a public class named `Scan1`. Everything inside the curly braces `{}` is part of this class. 23 | 24 | 3. **Main Method:** `public static void main(String[] args) { ... }` is the main method. It's where the program starts running. 25 | 26 | 4. **Creating a Scanner Object:** `Scanner input = new Scanner(System.in);` creates a Scanner object called `input` that will read user input from the keyboard. 27 | 28 | 5. **Prompting for Name:** 29 | - `System.out.print("Please Enter Your Name: ");` displays the message "Please Enter Your Name:" on the screen. 30 | - `String name = input.nextLine();` reads the user's input (their name) and stores it in a variable named `name`. 31 | 32 | 6. **Greeting the User:** `System.out.println("Good Morning " + name);` prints "Good Morning" followed by the user's name. 33 | 34 | 7. **Asking for Age:** 35 | - `System.out.print(name + ", tell me your age");` displays the message ", tell me your age" where `` is the name the user entered. 36 | - `int age = input.nextInt();` reads the user's input (their age) and stores it in a variable named `age`. 37 | 38 | 8. **Displaying Age:** `System.out.print("Your age is " + age);` prints "Your age is" followed by the user's age. 39 | 40 | In summary, this code asks the user for their name and age, then greets them and repeats their age back to them. */ -------------------------------------------------------------------------------- /Scan2.java: -------------------------------------------------------------------------------- 1 | /*In Java, there are several ways to take input from the user. Here are some of the most common methods: 2 | 3 | 1. **Using Scanner Class:** 4 | The `Scanner` class is a part of the `java.util` package and is used to obtain input of primitive types like `int`, `double`, etc., as well as strings. 5 | ```java 6 | import java.util.Scanner; 7 | 8 | public class InputExample { 9 | public static void main(String[] args) { 10 | Scanner scanner = new Scanner(System.in); 11 | System.out.print("Enter your name: "); 12 | String name = scanner.nextLine(); 13 | System.out.print("Enter your age: "); 14 | int age = scanner.nextInt(); 15 | System.out.println("Name: " + name + ", Age: " + age); 16 | } 17 | } 18 | ``` 19 | 20 | 2. **Using BufferedReader Class:** 21 | The `BufferedReader` class can be used to read text from an input stream (like the console). It is often wrapped around an `InputStreamReader`. 22 | ```java 23 | import java.io.BufferedReader; 24 | import java.io.InputStreamReader; 25 | import java.io.IOException; 26 | 27 | public class InputExample { 28 | public static void main(String[] args) throws IOException { 29 | BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 30 | System.out.print("Enter your name: "); 31 | String name = reader.readLine(); 32 | System.out.print("Enter your age: "); 33 | int age = Integer.parseInt(reader.readLine()); 34 | System.out.println("Name: " + name + ", Age: " + age); 35 | } 36 | } 37 | ``` 38 | 39 | 3. **Using Console Class:** 40 | The `Console` class provides methods to read text from the console. It can handle both password and standard input. 41 | ```java 42 | public class InputExample { 43 | public static void main(String[] args) { 44 | java.io.Console console = System.console(); 45 | if (console != null) { 46 | String name = console.readLine("Enter your name: "); 47 | int age = Integer.parseInt(console.readLine("Enter your age: ")); 48 | System.out.println("Name: " + name + ", Age: " + age); 49 | } else { 50 | System.out.println("No console available"); 51 | } 52 | } 53 | } 54 | ``` 55 | 56 | 4. **Using JOptionPane (for GUI-based input):** 57 | The `JOptionPane` class provides methods to create dialog boxes for input and output. 58 | ```java 59 | import javax.swing.JOptionPane; 60 | 61 | public class InputExample { 62 | public static void main(String[] args) { 63 | String name = JOptionPane.showInputDialog("Enter your name:"); 64 | String ageString = JOptionPane.showInputDialog("Enter your age:"); 65 | int age = Integer.parseInt(ageString); 66 | JOptionPane.showMessageDialog(null, "Name: " + name + ", Age: " + age); 67 | } 68 | } 69 | ``` 70 | 71 | 5. **Using DataInputStream (not recommended for new code):** 72 | The `DataInputStream` class was used in older code but is not recommended for new code as it's more low-level and less convenient than `Scanner` or `BufferedReader`. 73 | ```java 74 | import java.io.DataInputStream; 75 | import java.io.IOException; 76 | 77 | public class InputExample { 78 | public static void main(String[] args) throws IOException { 79 | DataInputStream dis = new DataInputStream(System.in); 80 | System.out.print("Enter your name: "); 81 | String name = dis.readLine(); 82 | System.out.print("Enter your age: "); 83 | int age = Integer.parseInt(dis.readLine()); 84 | System.out.println("Name: " + name + ", Age: " + age); 85 | } 86 | } 87 | ``` 88 | 89 | Each of these methods has its own use cases and advantages. The `Scanner` class is the most commonly used method for console input due to its ease of use and flexibility. */ -------------------------------------------------------------------------------- /Scan3.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Scan3 { 4 | 5 | public static void main(String[] args) { 6 | 7 | Scanner input = new Scanner (System.in); 8 | System.out.print("Enter Your Name: "); 9 | String name = input.nextLine(); 10 | System.out.println("Thank You " +name); 11 | System.out.print("Tell Me Your Age"); 12 | int age = input.nextInt(); 13 | System.out.println("Hello, " +name); 14 | System.out.println("I Know Your details: "); 15 | System.out.println("Your Name is " +name); 16 | System.out.println("Your Age is "+age); 17 | 18 | } 19 | } 20 | 21 | /*Sure, let's go through the code step by step: 22 | 23 | 1. **Importing Scanner Class**: 24 | ```java 25 | import java.util.Scanner; 26 | ``` 27 | This line imports the `Scanner` class from the `java.util` package, which is used to get user input. 28 | 29 | 2. **Class Declaration**: 30 | ```java 31 | public class Scan3 { 32 | ``` 33 | This line declares a public class named `Scan3`. 34 | 35 | 3. **Main Method**: 36 | ```java 37 | public static void main(String[] args) { 38 | ``` 39 | This is the main method, which is the entry point of the program. The program starts executing from here. 40 | 41 | 4. **Creating Scanner Object**: 42 | ```java 43 | Scanner input = new Scanner(System.in); 44 | ``` 45 | This line creates a `Scanner` object named `input` that reads from the standard input (keyboard). 46 | 47 | 5. **Prompting and Reading User's Name**: 48 | ```java 49 | System.out.print("Enter Your Name: "); 50 | String name = input.nextLine(); 51 | System.out.println("Thank You " + name); 52 | ``` 53 | - `System.out.print("Enter Your Name: ");` displays the prompt "Enter Your Name: " without a newline. 54 | - `String name = input.nextLine();` reads the entire line of text input by the user and stores it in the variable `name`. 55 | - `System.out.println("Thank You " + name);` displays "Thank You" followed by the user's name. 56 | 57 | 6. **Prompting and Reading User's Age**: 58 | ```java 59 | System.out.print("Tell Me Your Age"); 60 | int age = input.nextInt(); 61 | ``` 62 | - `System.out.print("Tell Me Your Age");` displays the prompt "Tell Me Your Age". 63 | - `int age = input.nextInt();` reads an integer input by the user and stores it in the variable `age`. 64 | 65 | 7. **Displaying User Details**: 66 | ```java 67 | System.out.println("Hello, " + name); 68 | System.out.println("I Know Your details: "); 69 | System.out.println("Your Name is " + name); 70 | System.out.println("Your Age is " + age); 71 | ``` 72 | - `System.out.println("Hello, " + name);` displays "Hello, " followed by the user's name. 73 | - `System.out.println("I Know Your details: ");` displays "I Know Your details: ". 74 | - `System.out.println("Your Name is " + name);` displays "Your Name is " followed by the user's name. 75 | - `System.out.println("Your Age is " + age);` displays "Your Age is " followed by the user's age. 76 | 77 | In summary, this program prompts the user to enter their name and age, then it reads and stores these inputs, and finally, it displays the entered name and age along with some custom messages. */ -------------------------------------------------------------------------------- /ScanSubInt.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class ScanSubInt { 4 | 5 | public static void main (String[] args) { 6 | 7 | Scanner input = new Scanner(System.in); 8 | System.out.print("Enter Your Two Number: "); 9 | int num1 = input.nextInt(); 10 | int num2 = input.nextInt(); 11 | int num3 = num1-num2; 12 | 13 | System.err.println("Your Answer is " +num3); 14 | 15 | if (num1>num2) { 16 | System.out.println(num3); 17 | } 18 | else { 19 | System.err.println("Your Answer will be negative because You Given me First digit greater than Second digit " +num3); 20 | } 21 | 22 | 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /ShortDeclaration.java: -------------------------------------------------------------------------------- 1 | public class ShortDeclaration { 2 | public static void main(String[] args) { 3 | // Direct declaration and initialization 4 | short shortLiteral = 100; 5 | 6 | // Type casting from an int 7 | int intValue = 100; 8 | short shortCast = (short) intValue; 9 | 10 | // Using arithmetic expressions 11 | short shortCalculation = (short) (50 + 50); 12 | 13 | // Using the Short wrapper class 14 | Short shortObject = 100; // Autoboxing 15 | short shortFromObject = shortObject; // Unboxing 16 | 17 | // Using String parsing 18 | short shortFromString = Short.parseShort("100"); 19 | 20 | // Print values 21 | System.out.println("shortLiteral: " + shortLiteral); 22 | System.out.println("shortCast: " + shortCast); 23 | System.out.println("shortCalculation: " + shortCalculation); 24 | System.out.println("shortObject: " + shortObject); 25 | System.out.println("shortFromObject: " + shortFromObject); 26 | System.out.println("shortFromString: " + shortFromString); 27 | } 28 | } 29 | 30 | /*This Java program demonstrates various ways to declare and use `short` variables. Here’s a detailed explanation of each part of the code: 31 | 32 | ### Code Explanation 33 | 34 | 1. **Class Declaration and Main Method:** 35 | ```java 36 | public class ShortDeclaration { 37 | public static void main(String[] args) { 38 | ``` 39 | - The class `ShortDeclaration` is defined. 40 | - The `main` method is the entry point of the program. 41 | 42 | 2. **Direct Declaration and Initialization:** 43 | ```java 44 | short shortLiteral = 100; 45 | ``` 46 | - A `short` variable `shortLiteral` is declared and initialized with a literal value `100`. 47 | 48 | 3. **Type Casting from an int:** 49 | ```java 50 | int intValue = 100; 51 | short shortCast = (short) intValue; 52 | ``` 53 | - An `int` variable `intValue` is declared and initialized with a value `100`. 54 | - The `int` value is explicitly cast to a `short` using `(short)` before assignment to the `short` variable `shortCast`. 55 | 56 | 4. **Using Arithmetic Expressions:** 57 | ```java 58 | short shortCalculation = (short) (50 + 50); 59 | ``` 60 | - An arithmetic expression `50 + 50` is evaluated to produce the result `100`. 61 | - The result, which is of type `int` by default, is explicitly cast to `short` before assignment to the `short` variable `shortCalculation`. 62 | 63 | 5. **Using the Short Wrapper Class:** 64 | ```java 65 | Short shortObject = 100; // Autoboxing 66 | short shortFromObject = shortObject; // Unboxing 67 | ``` 68 | - A `Short` object `shortObject` is initialized with a `short` value `100`. This process is known as autoboxing, where a primitive `short` is automatically converted to a `Short` object. 69 | - The `short` value is retrieved from the `Short` object using unboxing, where the `Short` object is automatically converted to a primitive `short`. 70 | 71 | 6. **Using String Parsing:** 72 | ```java 73 | short shortFromString = Short.parseShort("100"); 74 | ``` 75 | - A `short` variable `shortFromString` is initialized by parsing the string `"100"` using `Short.parseShort()`, which converts the string to a `short`. 76 | 77 | 7. **Printing Values:** 78 | ```java 79 | System.out.println("shortLiteral: " + shortLiteral); 80 | System.out.println("shortCast: " + shortCast); 81 | System.out.println("shortCalculation: " + shortCalculation); 82 | System.out.println("shortObject: " + shortObject); 83 | System.out.println("shortFromObject: " + shortFromObject); 84 | System.out.println("shortFromString: " + shortFromString); 85 | ``` 86 | - Each of the `short` variables is printed to the console using `System.out.println`. 87 | 88 | ### Output Explanation 89 | When this program is run, it will print the following: 90 | 91 | ``` 92 | shortLiteral: 100 93 | shortCast: 100 94 | shortCalculation: 100 95 | shortObject: 100 96 | shortFromObject: 100 97 | shortFromString: 100 98 | ``` 99 | 100 | - **shortLiteral:** Prints the value `100` assigned using the short literal. 101 | - **shortCast:** Prints the value `100` which was type cast from an `int` variable. 102 | - **shortCalculation:** Prints the result of the addition calculation, which is `100`. 103 | - **shortObject:** Prints the value `100` stored in the `Short` object due to autoboxing. 104 | - **shortFromObject:** Prints the value `100` extracted from the `Short` object due to unboxing. 105 | - **shortFromString:** Prints the value `100` obtained from parsing the string `"100"`. 106 | 107 | This program effectively demonstrates the different methods to declare and work with `short` variables in Java. */ -------------------------------------------------------------------------------- /ShortHand.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class ShortHand { 3 | public static void main(String[] args) { 4 | 5 | Scanner A0 = new Scanner (System.in); 6 | System.out.println("Give me 1st number: "); 7 | int a = A0.nextInt(); 8 | System.out.println("Give me 2nd number: "); 9 | int b = A0.nextInt(); 10 | 11 | System.out.println("Addition: " + (a +=b)); 12 | System.out.println("Substraction: " +(a -=b)); 13 | System.out.println("Multiplication: " +(a *=b)); 14 | System.out.println("Division: " +(a /=b)); 15 | System.out.println("Remainder: " +(a %=b)); 16 | } 17 | } -------------------------------------------------------------------------------- /SwapValue1.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.*; 3 | public class SwapValue1 { 4 | 5 | public static void main (String args[]) { 6 | Scanner Swap = new Scanner (System.in); 7 | 8 | System.out.print("Give Me Your 1st Number: "); 9 | int a = Swap.nextInt(); 10 | System.out.print("Give Me Your 2nd Number: "); 11 | int b = Swap.nextInt(); 12 | 13 | int c; 14 | 15 | c=b; 16 | b=a; 17 | 18 | System.out.println("1st Number " +c); 19 | System.out.println("2nd Number " +b); 20 | 21 | 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /SwapValue2.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | 5 | public class SwapValue2 { 6 | 7 | public static void main(String[] args) { 8 | 9 | Scanner SwapNum = new Scanner (System.in); 10 | System.out.print("Enter Your Two Number: "); 11 | int Num1 = SwapNum.nextInt(); 12 | int Num2 = SwapNum.nextInt(); 13 | 14 | System.out.println("Enter Your Number: " +Num1 +Num2); 15 | 16 | 17 | 18 | 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /UnaryOpera.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class UnaryOpera { 3 | public static void main(String[] args) { 4 | Scanner unary = new Scanner(System.in); 5 | System.out.println("Enter Your Number: "); 6 | int a = unary.nextInt(); 7 | int b, c, d, e, f; 8 | 9 | b = -a; 10 | c = ++a; 11 | d = --a; 12 | e = a++; 13 | f = a--; 14 | 15 | System.out.println("Value is: " +b); 16 | System.out.println("Value is: " +c); 17 | System.out.println("Value is: " +d); 18 | System.out.println("Value is: " +e); 19 | System.out.println("Value is: " +f); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /declareFloat.java: -------------------------------------------------------------------------------- 1 | public class declareFloat { 2 | public static void main(String[] args) { 3 | // Using float literal with 'f' or 'F' suffix 4 | float floatLiteral1 = 10.5f; 5 | float floatLiteral2 = 20.3F; 6 | 7 | // Using type casting from a double value 8 | float floatCast = (float) 10.5; 9 | 10 | // Using the result of a calculation 11 | float floatCalculation = 10.0f / 2; 12 | 13 | // Using the Float class 14 | Float floatObject = 10.5f; // Autoboxing 15 | float floatFromObject = floatObject; // Unboxing 16 | 17 | // Print values 18 | System.out.println("floatLiteral1: " + floatLiteral1); 19 | System.out.println("floatLiteral2: " + floatLiteral2); 20 | System.out.println("floatCast: " + floatCast); 21 | System.out.println("floatCalculation: " + floatCalculation); 22 | System.out.println("floatObject: " + floatObject); 23 | System.out.println("floatFromObject: " + floatFromObject); 24 | } 25 | } 26 | 27 | /* This Java program demonstrates different ways to declare and use `float` variables. Here’s a step-by-step explanation of the code: 28 | 29 | ### Code Explanation 30 | 31 | 1. **Class Declaration:** 32 | ```java 33 | public class FloatDeclaration { 34 | public static void main(String[] args) { 35 | ``` 36 | - The class `FloatDeclaration` is defined. 37 | - The `main` method is the entry point of the program. 38 | 39 | 2. **Using float literals with 'f' or 'F' suffix:** 40 | ```java 41 | float floatLiteral1 = 10.5f; 42 | float floatLiteral2 = 20.3F; 43 | ``` 44 | - Two `float` variables, `floatLiteral1` and `floatLiteral2`, are declared and initialized with literal values. 45 | - The suffix 'f' or 'F' indicates that these literals are of type `float`. 46 | 47 | 3. **Using type casting from a double value:** 48 | ```java 49 | float floatCast = (float) 10.5; 50 | ``` 51 | - A `double` literal `10.5` is explicitly cast to `float` using `(float)` before assignment to the `float` variable `floatCast`. 52 | 53 | 4. **Using the result of a calculation:** 54 | ```java 55 | float floatCalculation = 10.0f / 2; 56 | ``` 57 | - A calculation `10.0f / 2` is performed. Since `10.0f` is a `float` literal, the result is also a `float`. 58 | - The result of the calculation is assigned to `floatCalculation`. 59 | 60 | 5. **Using the `Float` class:** 61 | ```java 62 | Float floatObject = 10.5f; // Autoboxing 63 | float floatFromObject = floatObject; // Unboxing 64 | ``` 65 | - A `Float` object `floatObject` is initialized with a `float` value `10.5f`. This is known as autoboxing, where a primitive `float` is automatically converted to a `Float` object. 66 | - The `float` value is retrieved from the `Float` object using unboxing, where the `Float` object is automatically converted to a primitive `float`. 67 | 68 | 6. **Printing values:** 69 | ```java 70 | System.out.println("floatLiteral1: " + floatLiteral1); 71 | System.out.println("floatLiteral2: " + floatLiteral2); 72 | System.out.println("floatCast: " + floatCast); 73 | System.out.println("floatCalculation: " + floatCalculation); 74 | System.out.println("floatObject: " + floatObject); 75 | System.out.println("floatFromObject: " + floatFromObject); 76 | ``` 77 | - Each of the `float` variables is printed to the console using `System.out.println`. 78 | 79 | ### Output Explanation 80 | When this program is run, it will print the following: 81 | 82 | ``` 83 | floatLiteral1: 10.5 84 | floatLiteral2: 20.3 85 | floatCast: 10.5 86 | floatCalculation: 5.0 87 | floatObject: 10.5 88 | floatFromObject: 10.5 89 | ``` 90 | 91 | - **floatLiteral1:** Prints the value `10.5` assigned using the literal with 'f' suffix. 92 | - **floatLiteral2:** Prints the value `20.3` assigned using the literal with 'F' suffix. 93 | - **floatCast:** Prints the value `10.5` which was type cast from a `double` literal. 94 | - **floatCalculation:** Prints the result of the division calculation, which is `5.0`. 95 | - **floatObject:** Prints the value `10.5` stored in the `Float` object due to autoboxing. 96 | - **floatFromObject:** Prints the value `10.5` extracted from the `Float` object due to unboxing. 97 | 98 | This program effectively demonstrates the different methods to declare and work with `float` variables in Java. 99 | */ -------------------------------------------------------------------------------- /declaringInt.java: -------------------------------------------------------------------------------- 1 | //How to declare Integers 2 | 3 | public class declaringInt { 4 | 5 | public static void main(String[] args) { 6 | 7 | int myNumber = 55; 8 | 9 | int newNumber; 10 | 11 | newNumber = 85; 12 | 13 | newNumber = 87; 14 | 15 | myNumber = newNumber; 16 | 17 | System.out.println(myNumber); 18 | System.out.println(newNumber); 19 | } 20 | 21 | } 22 | 23 | /* Let's go through the `declaringInt` class code line by line: 24 | 25 | ```java 26 | public class declaringInt { 27 | ``` 28 | - This line declares a public class named `declaringInt`. In Java, a class is a blueprint for objects that defines a datatype by bundling data and methods that work on the data. 29 | 30 | ```java 31 | public static void main(String[] args) { 32 | ``` 33 | - This line declares the `main` method, which is the entry point of any Java application. The `public` keyword means it can be accessed from outside the class. `static` means this method can be called without creating an instance of the class. `void` means this method does not return a value. `String[] args` is an array of strings that stores command-line arguments. 34 | 35 | ```java 36 | int myNumber = 55; 37 | ``` 38 | - This line declares an integer variable named `myNumber` and initializes it with the value `55`. 39 | 40 | ```java 41 | int newNumber; 42 | ``` 43 | - This line declares another integer variable named `newNumber` but does not initialize it. 44 | 45 | ```java 46 | newNumber = 85; 47 | ``` 48 | - This line assigns the value `85` to the `newNumber` variable. 49 | 50 | ```java 51 | newNumber = 87; 52 | ``` 53 | - This line reassigns the value `87` to the `newNumber` variable, overwriting the previous value (`85`). 54 | 55 | ```java 56 | myNumber = newNumber; 57 | ``` 58 | - This line assigns the value of `newNumber` (which is `87`) to `myNumber`. Now, `myNumber` also holds the value `87`. 59 | 60 | ```java 61 | System.out.println(myNumber); 62 | ``` 63 | - This line prints the current value of `myNumber` to the console. Since `myNumber` was assigned the value of `newNumber`, it will print `87`. 64 | 65 | ```java 66 | System.out.println(newNumber); 67 | ``` 68 | - This line prints the current value of `newNumber` to the console, which is `87`. 69 | 70 | ```java 71 | } 72 | ``` 73 | - This line closes the `main` method. 74 | 75 | ```java 76 | } 77 | ``` 78 | - This line closes the `declaringInt` class. 79 | 80 | When you run this program, the output will be: 81 | ``` 82 | 87 83 | 87 84 | ``` 85 | 86 | This is because both `myNumber` and `newNumber` end up holding the value `87` before they are printed. */ 87 | --------------------------------------------------------------------------------