**JAVA INTRODUCTION**
📊 Java is a flexible language widely used in business enterprises, Android apps,
and games, offering opportunities for Java developers with an average starting
salary of $70,000.
💻 Source code is written in human-readable format and compiled into machine-
specific machine code. Java solves this by compiling source code to cross-platform
bytecode.
☕ A Java Virtual Machine (JVM) is necessary to translate bytecode to machine code.
You can obtain a JVM by downloading a Java Development Kit (JDK; developer tools),
which includes a JVM(runs java programs) and other development tools. (JRE= Java
Runtime Environment; stores libraries and toolkits
🌟 Recommended Integrated Development Environments (IDEs) for Java include Eclipse
and IntelliJ IDEA, providing a user-friendly interface for writing, checking,
compiling, and running code.
💼 Creating a Java project in an IDE is the first step to writing Java programs.
Configuring the Java Runtime Environment (JRE) within the project settings is
essential.
️ A Java class is a collection of related code, and the main method is crucial for
program execution. Code within the main method is executed sequentially, starting
from the top.
📝 Using `System.out.println()` displays text in the console window and moves to the
next line. The escape sequence `\n` also creates a new line, while `\t` adds a tab,
and `\"` displays double quotes.
️️ Comments in Java, indicated by `//` for single-line comments and `/* ... */` for
multi-line comments, are ignored by the compiler and are useful for documentation
and notes.
🌈 You can customize your Java IDE's appearance by changing the theme, font color,
and background color to suit your preferences.
✨ IDE shortcuts, like `sysout`, can automate the generation of
`System.out.println()` statements. The IDE also offers a Find and Replace feature
to make text replacements easier.
🔍 Keyboard shortcuts like `Ctrl + -` to zoom out and `Ctrl + +` to zoom in, as well
as reopening closed views in the IDE, can improve your workflow and productivity.
___________________________________________________________________________________
___________________________________
**VARIABLES IN JAVA**
💡 Variables in Java serve as placeholders for values, behaving as the value they
contain.
💡 There are eight primitive data types: boolean, byte, short, int, long, float,
char, and string (a reference data type).
💡 Data types have different sizes and capabilities, such as storing whole numbers,
fractional numbers, characters, or sequences of characters.
💡 Declaration, assignment, and initialization are key steps in creating and using
variables.
💡 Floats and doubles are used for fractional numbers, with doubles offering greater
precision.
💡 Boolean variables can hold only true or false values.
💡 Char variables store single characters, enclosed in single quotes.
💡 Strings are reference data types, used to store sequences of characters.
💡 Understanding data type conventions, such as using "f" for floats and "L" for
longs, is essential.
💡 Proper variable usage and concatenation are demonstrated for each data type.
___________________________________________________________________________________
_____________________________________
📊 Variables in programming behave as the value they contain, just like algebraic
variables.
🧮 There are eight primitive data types in Java, including boolean, byte, short,
int, long, float, double, and char.
🥇 Floats and doubles can store fractional numbers, with doubles having more
precision.
📝 Strings are reference data types and can store sequences of characters like words
or sentences.
📊 Primitive data types use less memory and are faster compared to reference data
types.
* Primitive (8 data types) & Reference(Unlimited data types(User Defined))
* Primitive(Stores data) & Reference(Stores address)
*Primitive(can hold only one value) & Reference(Could hold more than one value)
️️ To create a variable in Java, declare the data type(int x; ), assign a value (x=
12) , or initialize both in one step( int x= 123; ).
💬 Variables can be printed to the console to display their values.
🌐 The long data type is useful for storing extremely large numbers.
🎯 Floats and doubles can store numbers with decimal portions.
✅ Booleans can only hold true or false values.
️️ Strings start with a capital 'S' and can store text sequences.
___________________________________________________________________________________
_____________________________________
🧊 Variables X and Y are initialized with the string values "water" and "kool-aid"
respectively.
🔄 Directly assigning X to Y or Y to X doesn't work for swapping their values.
🔄 To swap variables X and Y, create a temporary variable (temp), store one
variable's value in temp, then assign the other variable's value to the first
variable and temp's value to the second variable.
🤝 Swapping variables manually with a temporary variable (temp) is a common
technique when a programming language doesn't provide a direct swap function.
CODE:
public class Main {
public static void main(String[] args) {
String x = "water";
String y = "Kool-Aid";
String temp;
temp = x;
x=y;
y=temp;
System.out.println("x: "+x);
System.out.println("y: "+y);
}
}
___________________________________________________________________________________
___________________________________
HOW TO TAKE INPUT IN JAVA:
️️ Import the Scanner class from the java.util package to use it for user input.
📝 Create a Scanner object and use it to prompt and accept user input for strings
and integers.
❌ Be cautious about input data types to avoid input mismatches.
🔄 Use nextLine() method to clear the Scanner after reading other data types.
___________________________________________________________________________________
___________________________________
️️ To accept user input in Java, you need to use the `Scanner` class, which is part
of the `java.util` package.
🤖 Create a `Scanner` object to accept user input: `Scanner scanner = new
Scanner(System.in);`
🔄 Use `scanner.nextLine()` to accept a line of text as user input and store it in a
variable.
🔄 Ensure that the data type of the input matches what you expect (e.g., string for
text, integer for numbers).
🤖 Youcan accept different types of input, such as strings and integers, using the
appropriate methods (`nextLine()` and `nextInt()`).
🔄 When using `nextLine()` after `nextInt()`, consider adding an extra
`scanner.nextLine()` to clear any remaining newline characters in the input buffer.
🧮 An expression in Java consists of operands (values, variables, numbers) and
operators (e.g., +, -, *, /, %).
🤖 You can use arithmetic expressions to manipulate variables. For example, to
increment a variable called `friends`, use `friends = friends + 1`.
➗ The modulus operator (%) gives you the remainder of division, e.g., `10 % 3`
results in 1 because 10 divided by 3 has a remainder of 1.
🔄 Java provides shorthand operators like `++` for increment and `--` for decrement.
For example, `friends++` increments the `friends` variable by one.
️️ Integer division truncates the decimal portion, and you can cast the result to
another data type (e.g., double) to retain the decimal part.
___________________________________________________________________________________
_________________________________
📊 You can create a basic graphical user interface (GUI) application in Java using
`JOptionPane` to display dialog boxes.
📝 Import `javax.swing.JOptionPane` to work with `JOptionPane` class.
📥 Use `JOptionPane.showInputDialog` to create an input dialog box, and store the
result as a string.
📤 Use `JOptionPane.showMessageDialog` to display a message dialog box.
🧮 When using `JOptionPane.showInputDialog`, remember to convert the result to the
appropriate data type (e.g., `Integer.parseInt` for integers).
🌟 You can work with different data types, such as doubles, by parsing the input
appropriately.
🚀 This basic GUI is just an introduction; more advanced GUI topics will be covered
later in the playlist.
Code:
import javax.swing.JOptionPane;
public class FirstClass {
public static void main(String [] args) {
String name= JOptionPane.showInputDialog("Enter your name");
JOptionPane.showMessageDialog(null, "Hello "+ name);
int age= Integer.parseInt(JOptionPane.showInputDialog("Enter your age"));
JOptionPane.showMessageDialog(null, "You are "+ age + " years old");
double height= Double.parseDouble(JOptionPane.showInputDialog("Enter your
height"));
JOptionPane.showMessageDialog(null, "You are "+ height+ "cm tall");
}
}
___________________________________________________________________________________
___________________________________
MATHS CLASS IN JAVA:
🔄 The `Math.max(x, y)` method returns the larger of two numbers.
📈 The `Math.min(x, y)` method returns the smaller of two numbers.
➕ The `Math.abs(y)` method calculates the absolute value of a number.
√ The `Math.sqrt(x)` method finds the square root of a number.
🔄 `Math.round(x)` rounds a number to the nearest integer.
️️ `Math.ceil(x)` always rounds a number up.
️️ ️ `Math.floor(x)` always rounds a number down.
📐 You can use these math functions in practical programs, like calculating the
hypotenuse of a triangle.
📝 Remember to close the `Scanner` object after using it in your program.
Code for printing hypotenuse:
import java.util.*;
public class FirstClass {
public static void main(String [] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter values of base and perpendicular");
double a= sc.nextDouble();
double b= sc.nextDouble();
double c= Math.sqrt((a*a)+(b*b));
System.out.println("The value of hypotenuse is :"+ c);
sc.close();
}
}
___________________________________________________________________________________
___________________________________
JAVA RANDOM NUMBERS:
🎲 Import the `Random` class from `java.util` to generate random values.
🔄 Create an instance of the `Random` class to use for generating random values.
Random random = new Random();
🎲 Use `random.nextInt(limit)` to generate random integers within a specified limit.
🎲 `random.nextDouble()` generates random double values between 0 and 1.
🔄 `random.nextBoolean()` generates random boolean values (true or false).