Why You Must Learn Java?
One of the most popular languages. java Currently runs on 60.00.00.00,000 devices.
Wide usage (web Apps, Backend, Mobile, enterprise software).
High paying and lots of flexible
Object Oriented
Rich APIs and Communication Support.
What is a Programming language?
Humans use natural language like Hindi/ English.
computers only understand 0 and 1.
Giving Instructions to as a computer.
Instruction: Tell the computer what to do.
These Instructions are called codes.
Human Interaction is given in High-level languages.
Compiler converts High-level; language to Low-level Language.
What is An Algorithm?
Instructing Computer.
Step-by-step instructions to solve a particular task.
Example: instructions about How to Make Tea?
What is Syntax?
Structure of Words in a sentence.
Rules of Langauge
For Programming exact syntax must be followed.
History of Java
Developed by James Gosling at Sun Microsystems (Early and Originally named “oak”, later renamed Java in 1995)
First Release (1995): Introduced “Write Once, Run Anywhere” concept with cross-platform Compatibility.
JVM converts Byte code into Machine Code.
Developed with a vision of backward compatibility, Should break into new version releases.
Rapid Growth and Diversification: (The late 1990s -2010): Expanded from web apples to server-side applications; standardized into different editable for various computable programs.
How Java Changed The Internet?
Portability with Write Once Run Anytime.
Security because it is running on a virtual Machine.
JavaBuzzwords
Java is robust due to its strong memory management, exception handling, and type-checking Mechanism which helps in preventing more effective processing and Task management.
Multithreading in programming is the ability of the CPU to execute multiple threads concurrently. Allows for more efficient processing and Task Management.
Java is Architectually neural because the compiled code bytecode can run on any device with Java Virtual Machine, regardless of the Hardware Architecture.
Java combined high performance with interpretability, as its bytecode is interpreted by the Java Virtual Machine (JVM), which is just-in_time (JIT) commands for efficiency and Framework.
Java is inherently;\ distributed and designed to facilitate Network-based application development and interaction, seamlessly integrating with Internet protocols and remote method invocation.
Java is the Object object-oriented programming.
(2) Java Basics
Installing JDK
Search JDK download
Make Sure to download from the Oracle website.
Download the latest Version.
Website Address for Java Download: https://www.java.com/en/download/
(1) Programm java
(2) Java Compiler
(3) Program.class
(4) JVM
(5) Program
(6) Java Source File
(7) Javac
(8) Bytecode .class file
(9) Java
(10) Output
File Extensions
.JAVA
- Contains Java Source Code
- High-Level Human Readable
- Used For Development
- File is Editable
.Class
- Contains Java Bytecode
- For consumption of JVM
- Used for Execution
- Not Meant to be Edited
JDK to JVM and JRE
JDK
- It is a software kit required to develop Java Applications.
- Includes the JRE< an interpreter/Loader (java), a compiler (Javac), a Document(Javadoc), and other tools needed for Java Development.
- Essentially, JDX is a superset of JRE.
.JRE
- It’s part of the JDK but can be downloaded Separately.
- Provides the Libraries, the JVM, and other components to run the app.
- Does not have tools and utilities for developers like compilers or interpteres.
.JVM
It is part of JRE and is responsible for executing the bytecodes.
Ensure Java’s write-once-code anywhere capability.
Not Platform Independent a different JVM is needed for each type of OS.
Showing Output
Example | Result |
System.out.print(“one”); System.out.print(“two”); System.out.print(“Three”) | onetwothree |
System.out.println(“one”); System.out.println(“two”); System.out.println(“Three”) | one two three |
System.out.println(“Three”) | New Line |
Importance of Main Method:
Entry Point: It’s the Entry point of a Java Program, where the execution starts, Without the main method., The Java Virtual Machine(JVM) does not know where to begin without code.
*Public and Static: The main method must be public and static, ensuring it’s and to JVM’s without needing to initiate the class.
*Fixed Signature: The main method has a fixed signature: Public static main(string[] args), Deviating from the signature means the JVM way, as the starting point.
What is IDE?
- IDE stands for Integrated Development Environment.
- Software Suite that consolidates basic tools required for software development.
- The central hub for coding, finding problems, and testing.
- Designed to improve developer efficiency.
Why We Need an IDE?
1.Streamline Development
2. Inceraes Productivity
3. Simplifies the complex tasks
4. Offers a unified Workspace
5. IDE features
- code Autocomplete
- Syntax Highlighting
- Version Control
- Error Checking
Installing IDE (Intellij Idea)
Search Intellij EDA
Make Sure download the Community Version.
Keep Your software up to date.
Download Website : https://www.jetbrains.com/idea/?var=1
(Download Software)
You have to download the community version.
After downloading it install it on your local system.
public class Main { public static void main(String[] args) { System.out.println("*"); System.out.println("**"); System.out.println("***"); System.out.println("****"); System.out.println("*****"); } } Practice Exercise: 1.Computers understand high level languages like Java , C - > false 2. An Algorithmm is set of instructions to accomplish task -> True 3. Computer is smart enough to ignore incorrect syntax. 4. Java was released in 1992. - True 5. Jaava was named over a person who made a good coffee - false 6. Bytecode is platform Indepenedent : True 7. JDK is a part of JRE = False 8. It;s Optional to declare main method as public. = False 9. .class flle contains machine code = False (It contains Byte code)1 10, Println adds a new line at the end of the line. (true) Data Types, variables and Input: variables are like containers used for stroring data values. Variables has 2 parts : varible names, value. Variables are not of same size. Memoey Allocation: Every Character is Converted in the 0s and 1. int a = 5 Every Memory is in RAM. The storage of the variable is done in RAM. RAM has Memory cell which has locations. RAM cell Stores Address and name. int age =20; int - Data Type age= Variable, name 20 - value = Assigment Operator in RAM the The memory will be reserved for the variable age.
Size | Default Value | Type of value stored | |
byte | 1 Byte | 0 | .integral |
short | 2 Byte | 0 | Integral |
int | 4 Byte | 0 | Integral |
long | 8 Byte | 0L | Integral |
char | 2 byte | ‘\U0000″ | Character |
float | 4 byte | 0.0F | Decimal |
double | 8 byte | 0.0d | Decimal |
boolean | 1 bit(till JDL 1.3 uses i byte) | F | True or False |
Non Primitive Data Type: String, Arrays, Objects, etc
public class Main {
public static void main(String[] args) {
int myNumber= 865; (//initlization)
int YourNumber; (//Declaration)
System.out.println(yourNumber);
yourNumber=45;
boolean isVegetarian=false;
System.out.println(isVegetarian);
float myFloat=5; )//It will consider 5.0
System.out.println(myFloat);
}
Naminfg Convention:
Camel case
start with a lowercase letter, Capitalize the first letter of each subsequent word.
Example: myVariableName
Snake_case
Starts with lowercase letter.separate words with underscorre
Example: My_variable_name
Kebab_case
* All lowercase letters, seperate words with hyphens, Example: my variable name
Keep a Good and Short name:
Choose names that are descriptive, but not tooo long. It should make it easy to understand and variable;’s purpose.
Java uses the camel case/
Java Identifier Rules:
1. The only allowed characters for identifiers are all alphanumeric Character [AZ][az][0-9],$, and _
2.can’t use keywords or reserved words
3Identifiers should not start with digits (0-9)
4. java identifiers are case-senstive.
5. There is no limit on the length of the identifier but it is advisable to use the optimum length of 4-15 character only.
Escaping Sequence:
public class Main {
public static void main(String[] args) {
System.out.println(“Hello \KGcopding\…”); (//Escaping Sequence]])
}
Escape Sequence | Description |
\t | Insert a tab in the text at this point |
\b | Insert a backspace in the text at this point |
\n | Insert a newline in the text at this point |
\”’ | Insert a double Quote Character in the text at this point |
\\ | insert a backslash character in the text at this point. |
User Input
import java.util. Scannner public class UserInput{ public static void main(string[] args) { Scanner input= new Scanner(System.in) System.out.println("Enter Your Name:") String Name=input.nextline(): , System.out. println("Good Morning " + name) System.out. println(name + ", Also tell me your age) int age = input.nextInt(); System.out.Println("Your age is: " + age); } } Example: Create a Program to input name of the person amd respong with "Welcome to KG Coding" public class welcome{ public static void main (String[] args) { System.out.print("please enter your name: "): scanner input = new Scnner(System.in) String name = input.Nextline(): System.out.printLn("welcome" + name + "KG coding) } } Example: Write a program to add Two Numbers (sum) public class Sum { Public static void man (String[] args{ scanner input = new Scanner(system.in): System.out.printlln("Welcome to Our calculator"); int firt num = input.nextint(); System.out.print("Now.Please Enter the second Number: "); int sum = First Name + Second Num System.out.print("Now.Please Enter the second Number: "); } Type Conversion and casting Automatic Type Conversion (Wideling - Implicit) byte -> Short -> int -> long -> Float -> double Narrowing (explicit) byte <- Short <- int -< long <- float <- double public class TypeConversion { public static void main (String[] args) { float my float =5; System. out.Println(myFloat); int myInt= (int) 5.45 System. out.Println(myInt); } }
Question | Answer |
In Java, a variable’s name can start with a number | False |
Char in Java can store a single character | True |
Java variable names are case-sensitive | False |
100l is a valid literal in Java | True |
\d is an escape sequence in Java for digit Character | false |
Scanner class is used for trading console input | True |
In Java, an int can be automatically converted to a Byte | True |
In java, an int can be automatically converted to a Byte | True |
Scanner class can be used to read both primitive datatypes and strings | True |
Operator, If – Else & Number System
Assignment operator (=)
Assigns the tight hand operand’s value to the left-hand operand.
Example int a =5;
public class main {
public static void main (string[] args) {
int myInt=9;
int newInt = myInt;
System. out.println(newInt);
}
}
Example: Create a Program to Swap two numbers:
Public class Swap ( Public static Void main (String[] args) { scanner.input=new Scanner(System,in) System.out.println("Welcome to swapping station \n\n"); int a = input.next(int); System.out.println("Entern the value of A"); int b = input.next(int); System.out.println("Entern the value of A");
Arithmetic Operator
Operators | Meaning | Example | Result |
+ | Addition | $=2 | 6 |
– | Division | 4-2 | 82 |
* | <ultiplication | 4*2 | 2 |
/ | Divison | 4/2 | 2 |
% | Modulus Operator to ger remainder target division | 5%2 | 1 |
Public class Arithmatic {
public static void main (String[] args){
int a =8;
int b=22;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
System.out.println(a//b);
System.out.println(a%b);
}
Unary operators
– | x=-y |
Pre Increment | x=x+y |
pre Decrement | x= –y |
Post Increment | x=y++ |
Post Decrement | x= y– |
# If-Else Operator
If else:
- Syntax : uses if () {} to check a condition
- What is if : Executes Block if condition is True, Skips if false.
- What is else : Executes a block when the if condition is false.
4.Curly Braces can be ommited for single statements, but not recommended - If -else ladder : Multiple if and else if blocks, only one executes.
- Use Variable : Can store conditions in Variables for use in if statements.
public class LearningIf {
public static void main (String [] args) {
boolean isMale= true:
String name = “Bob”
if (isMale) {
System.out.println ("Mr," + name);
} else {
System.out.Println (:Mr." + name)
}
System.out.println("After if")
}
}
Or You can write this as this:
public class LearningIf {
public static void main (String [] args) {
boolean isMale= true:
String name = “Bob”
if (is Male) System.out.println(“Mr.” + name);
else System.out.Println(“Ms.” + name)
System.out.println(“After if”)
boolean is SeniorCitizen {
system.out.println(“Hello Senior Citizen)
if(isSeniorCitizen) {
System.out.println(“Hello Senior Citizen”)
} else if{
if(isAnAdult) {
System.out.println(“Hello Audit”)
} else {
System.out.println(“Hello Child”)
}
}
}
Relational Operators :
Equality
== Check Values Quality
=== Checks Vale and Type
in Equality
!= Checks value inequality
Relational
Greater than
< Less than
= Greater than Equal to
<= Less than Equal to
The order of Relational Operators is less than arithmetic operators.
public class Relational {
public static void main (String[] args) {
Scanner.input = new Scanner (System.in)
System.out.println(“Welcome to Driving License Portal”)
int age = input.nextint()
if(age>=18) {
system.out.println("You are eligible to ride")
} else {
system.out.println("Beta Cycle Chala")
}
}
Logical Operators :
- Types : && (AND), || (OR) , !(NOT)
- AND (&&) : All Conditions must be true for the result to be true.
- Of(||): only one condition must be true for the result to be true.
- NOT(!) : inverts the Boolean Value of the condition.
- Lower Priority than Math and Comparison Operators.
Public class Logical {
public static void main ( String [] args) {
scanner input = new Scanner(system.in)
sytem.out.println(“Welcome to Ticket Discount Calculator”)
sytem.out.println(“Please Enter Your age”)
int age = input.nextInt();
System.Out.println(“Are you a female? (true/false) “);
boolean ifFeamle = inpur.nextBoolean();
if(age<5) {
System.out.println("You got 50% Discount)
} elseif { (age >60) && !isFemale) {
System.out.println("You got 25% Discount)
} else {
System.out.println("You got 25% Discount)
}
Number System | Base Or Redix | Digits or Symbols |
Binary | 2 | 0,1 |
Octal | 8 | 0.1.2.3.4.5.6.7 |
Hexadecimal | 13 | 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F |
Decimal Number System:
* Decimal Number System is base-10 System that has ten digits : 0,1,2.3.4.5,6,7.8.9
* The Decimal Number System is said to base, or radix, 10 because tt uses 10 digits and the coefficients are multiplied by 10.
* This is the base that we often use in our day-to-day life.
* Example (7.392) 10, Where 7,392 is a shorthand Notification for what should be written.
Binary Number System
- Binary Number System:
- The coefficients of the binary number system have only two possible values: 0 and 1.
- Each coefficient a, is multiplied by a power of the radix, e.g. 2!, and the results are added to
- obtain the decimal equivalent of the number.
- Example (11010.11)2 Value in Decimal?
Decimal | Binary |
0 | 0000 |
1 | 0001 |
2 | 0010 |
3 | 0011 |
4 | 0100 |
5 | 0101 |
6 | 0110 |
7 | 0111 |
8 | 1000 |
9 | 1001 |
10 | 1010 |
11 | 1011 |
12 | 1100 |
13 | 1101 |
14 | 1110 |
16 | 1111 |
What is Loop?
- Code that runs multiple times based on condition.
- Repeated Execution of code.
- Loops Automate repetitive Tasks.
- Types of Loops: While, For, While, do- while
- Iterations: Number of times loops run
Types of Loops
(1) While Loop
While(condition):{
}
While(glass is not empty) {
drink water
}
while(sleep not) {
continue sleep
}
print the sum of n natural numbers: import java.util.Scanner; public class SumOfNaturalNumbers { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the value of n: "); int n = scanner.nextInt(); int sum = sumOfNaturalNumbers(n); System.out.println("The sum of the first " + n + " natural numbers is: " + sum); } public static int sumOfNaturalNumbers(int n) { return (n * (n + 1)) / 2; } }
Number Systems
- Printing Pattern
- Triangular Patterns
- Numerical Rectangular Pattern
- Numerical Triangular pattern
System to represent numbers in different forms with different base values.
color code and Memory Allocation – Hexa Decimal Number System (0-15)
Base 10 – Decimal Numbers
245 = 2 * 10^2 + 4 * 10 ^ 1 + 5 * 10^1
Base 2 – Binary Number Systems (0-1)
1001 = 1 * 2^3 + 0* 2^2 + 0 * 2^1 + 1* 2^0 = 8 +0 + 1 = 9
Public class binaryToDecimal{
Public static void main (string[] args]
Scanner sc = new Scanner(System.in)
int binary – num = sc.nextInt():
int ans=0;
int pw=1;
while(binary_num >0) {
int unit_digit = binary_ num % 10
and + (unit_digit* pw)
binary num /=10
pw+=2
}
System.out.println(ans)
}
}
Object Oriented Programming
/