Introduction To Java
Why Programming Language Introduced ?
Ans : 1) interface between human and machine.
2) works on 0 and 1, false and true, on and off.
3) a - 97-122, A-65-90, 1-1, 2-10
Why Java ?
Ans : 1) used for mobile app, web app, games, enterprise
2) having JVM tech. other lang like kotlin, scala, groovy also works on JVM.
so if you work on this tech then you can say that you work on java.
3) Secure.
4) you if know java you can learn other lang quickly.
5) used for testing, to develope android app.
What is java ?
Ans : 1) Java is object oriented programming language. developed by sun Microsystem of USA in 1991.
2) James Gosling inventor of java.
Features of Java ?
Ans : 1) Simple
2) platform-independent - write once run anywhere. due to JVM.
assembly lang are platform-dependent. it is low level lang.
3) Secure - No Pointer concept.
How java works ?
Ans :
1) Java compiles source code into bytecode(. class file) and then bytecode is interpreted into machine code.
JVM : JVM (Java Virtual Machine). It is called a virtual machine because it doesn't physically exist. It is a specification that provides a runtime environment in which Java bytecode can be executed. It can also run those programs which are written in other languages and compiled to Java bytecode.
JRE : JRE is Java Runtime Environment. The Java Runtime Environment is a set of software tools which are used for developing Java applications. It is used to provide the runtime environment. It is the implementation of JVM. It physically exists. It contains a set of libraries + other files that JVM uses at runtime.
JDK : JDK is Java Development Kit. The Java Development Kit (JDK) is a software development environment which is used to develop Java applications and applets. It physically exists. It contains JRE + development tools.
Editions of Java Platforms
They are as follows:
1. Java SE (Standard Edition):
- This edition is used to develop client-side app, desktop app , communication, and user interface.
Ex : Calculator , Notepad .(those application which are not need server or internet to play ).
2. Java EE (Enterprise Edition):
- This edition is used to develop server-side applications such as Java servlets, JavaServer Pages (JSP), and JavaServer Faces (JSF).
- It is use to create Enterprises Application or Distrubuted Application .
- Enterprise software, or enterprise application software, is computer software used by organizations rather than individual users.
- used to develop web-based, messaging, distributed, and enterprise applications.
Ex : Instagram , gmail .
3. Java ME (Micro Edition):
- This edition is used to develop applications for mobile devices, such as cell phones. It is also used to develop Personnel Digital Assistants, Setup Box, and printers applications.
- It use to create Mobile Base Application .
- Mobile Base Application means if we design & execute any Application using on the basis of mobile hardware then application is known as mobile base Application .
- we can both standalone & Enterprise application in Micro Edition.
Most Common question :
Comparison Index | C++ | Java |
---|---|---|
Platform-independent | C++ is platform-dependent. | Java is platform-independent. |
Mainly used for | C++ is mainly used for system programming. embbeded programming | Java is mainly used for application programming. It is widely used in Windows-based, web-based, enterprise, and mobile applications. |
eg. fitness tracker, medical devices | eg. insta, facebook, flipcart | |
Multiple inheritance | C++ supports multiple inheritance. | Java doesn't support multiple inheritance through class. It can be achieved by using interfaces in java. |
Pointers | C++ supports pointers. | you can't write the pointer program in java. |
Compiler and Interpreter | C++ uses compiler only. C++ is compiled and run using the compiler which converts source code into machine code so, C++ is platform dependen | Java uses both compiler and interpreter. Java source code is converted into bytecode at compilation time. The interpreter executes this bytecode at runtime and produces output. Java is interpreted that is why it is platform-independent. |
Call by Value and Call by reference | C++ supports both call by value and call by reference. | Java supports call by value only. |
Structure and Union | C++ supports structures and unions. | Java doesn't support structures and unions. |
Hardware | C++ is nearer to hardware. | Java is not so interactive with hardware. |
Object-oriented | C++ is an object-oriented language. However, in the C language, a single root hierarchy is not possible. | Java is also an object-oriented language. However, everything (except fundamental types) is an object in Java. It is a single root hierarchy as everything gets derived from java.lang.Object. |
short
data type in Java has a range of -32,768 to 32,767. Therefore, if you try to assign the value 32800
to a short
variable, it will result in a compilation error because it exceeds the allowed range for the short
data type. Data Types in Java
Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
- A. Examples of primitive data types :
int myNum = 5;
float myFloatNum = 5.99f;
char myLetter = 'D';
boolean myBool = true;
String myText = "Hello";
- Difference between char and String:
- char stores single character like a, b, c and String stores sequence of characters like "hello", "Good Morning".
B. Examples of non-primitive types :
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
1. Arrays: A collection of elements, all of the same type, stored in contiguous memory locations.
example : int[] numbers = {1, 2, 3, 4, 5};
2. Classes and Objects: Classes are blueprints for creating objects. Objects are instances of classes.
Example :
class Car {
String color;
String model;
int year;
Car(String c, String m, int y) {
color = c;
model = m;
year = y;
}
}
Car myCar = new Car("red", "Toyota", 2021);
Operators in Java
Operators are used to perform operations on variables and values.
public static void main(String[] args) {
int a = 10;
int b = 5;
System.out.println("Addition: " + (a + b)); // 15
System.out.println("Subtraction: " + (a - b)); // 5
System.out.println("Multiplication: " + (a * b)); // 50
System.out.println("Division: " + (a / b)); // 2
System.out.println("Modulus: " + (a % b)); // 0
}
}
Decision making Statements :
if (condition) {
// code to be executed if condition is true
}
The if-else
statement provides an alternative block of code to execute if the condition is false.
Syntax:
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
Example:
int num = -5;
if (num > 0) {
System.out.println("Number is positive.");
} else {
System.out.println("Number is non-positive.");
}
The if-else-if
ladder allows you to check multiple conditions and execute a block of code as soon as one of the conditions is true.
Syntax:
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition2 is true
} else {
// code to be executed if all conditions are false
}
Example:
int num = 0;
if (num > 0) {
System.out.println("Number is positive.");
} else if (num < 0) {
System.out.println("Number is negative.");
} else {
System.out.println("Number is zero.");
}
The switch
statement evaluates an expression and executes code blocks based on matching cases.
Evaluates the expression once and jumps directly to the matching case, executing its block and then exiting.
Syntax:
switch (expression) {
case value1:
// code to be executed if expression equals value1
break;
case value2:
// code to be executed if expression equals value2
break;
default:
// code to be executed if expression doesn't match any case
}
Example 1:
import java.util.Scanner;
public class Day1{
public static void main (String[] args){
System.out.println("Enter Day");
Scanner sc = new Scanner (System.in);
int number = sc.nextInt();
String dayname = " ";
switch(number){
case 1:
dayname = "Monday";
break;
case 2:
dayname = "Tuesday";
break;
default:
dayname = "Wednesday";
}
System.out.println("dayname " +dayname);
}
}
Example 2:
import java.util.Scanner;
public class Maths{
public static void main (String[] args){
System.out.println("Enter case");
Scanner sc = new Scanner(System.in);
int a=10;
int b=20;
int Calculate = 0;
int number = sc.nextInt();
switch(number){
case 1:
Calculate=a+b;
break;
case 2:
Calculate=a-b;
break;
default:
System.out.println("Done");
}System.out.println("Calculate"+Calculate);
}
}
Syntax for diff datatypes : int number = sc.nextInt();
char word = sc.next().charAt(0);
String words = sc.nextLine();
double number = sc.nextDouble();
boolean value = sc.nextBoolean();
The ternary operator (? :
) provides a concise way to evaluate a condition and assign a value based on the result.
Syntax:
variable = (condition) ? expression1 : expression2;
- If
condition
is true,variable
is assignedexpression1
. - If
condition
is false,variable
is assignedexpression2
.
Example:
int num = 10;
String result = (num > 0) ? "Positive" : "Non-positive";
System.out.println("Number is: " + result);
Example 2 :
public class TernaryOperationExample {
public static void main(String[] args) {
int num = 10;
int a = 5;
int b = 3;
// Using ternary operator to decide whether to add or subtract
int result = (num > 0) ? (a + b) : (a - b);
System.out.println("Result is: " + result);
}
}
The for
loop is used when the number of iterations is known or determined.
- Initialization: Initializes the loop control variable.
- Condition: Evaluates whether to continue the loop or exit.
- Update: Updates the loop control variable after each iteration.
The while
loop is used when the number of iterations is not known beforehand, and the loop continues as long as the specified condition is true
.
1.table of number
2. sum of even numbers from 1 to 10.
3. number divide by 3.
4. square, cube.
5. power.
Syntax:
while (condition) {
// code to be executed
}
Example:
int i = 0;
while (i < 5) {
System.out.println(i); // prints numbers 0 to 4
i++;
The do-while
loop is similar to the while
loop, but it guarantees at least one execution of the loop body because it checks the condition after executing the loop body.
Syntax:
do {
// code to be executed
} while (condition);
Example:
int i = 0;
do {
System.out.println(i); // prints numbers 0 to 4
i++;
} while (i < 5);
In addition to the basic loop structures, Java also provides loop control statements to alter the normal flow of loops:
break
statement: Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.for (int i = 0; i < 10; i++) { if (i == 5) { break; // exits the loop when i is 5 } System.out.println(i); }
Skips the current iteration of the loop and continues with the next iteration.
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) {
continue; // skips even numbers
}
System.out.println(i); // prints odd numbers (1, 3)
}
return
statement: Exits from the current method and optionally returns a value.
public int sum(int a, int b) {
return a + b; // returns the sum of a and b
}
public class DoWhileExample10 {
public static void main(String[] args) {
int number = 12345;
int reversed = 0;
do {
int digit = number % 10;
reversed = reversed * 10 + digit;
number /= 10;
} while (number != 0);
System.out.println("Reversed number: " + reversed);
}
}
Arrays:
An array is a collection of similar type of elements which has contiguous memory location.
Java array is an object which contains elements of a similar data type. Additionally, The elements of an array are stored in a contiguous memory location. It is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array.
declaration of array :String [] cars;
initialization of array :String[] cars = {"creta", "verna", "BMW"};
initialization of integer array :int[] numbers = {1,5,33,68,2};
To change the specific
cars[0] = "Tata Safari"
Comments
Post a Comment