JAVA TUTORIAL
JAVA DEFINTION:
* Java is a popular programming language,Created in 1995.
*It is owend by Oracle,and more than 3billion device run in java.
*Java is used to develop mobile application, web application, Desktop application, Games and so on.
- Public --> Access modifier. It means the class can be accessed from anywhere in the program.
- Class --> Keyword used to create a class in java.
- name -->Class name. You can replace NAME with any vaild class name such as Hello, Student, etc..
- The method can be accessed by the JVM (Java Virtual Machine).
- Allows the method to be called without creating an object of the class.
- The method does not return any value.
- Specail method name recognized by the JVM as the entry point of the program
- Stores command-line arguments passed when running the program.
- args is an array of string objects.
3. System.out.println(" ");
- A built_in java class
- Standard output steam (console).
- Prints the text and moves the cursor to the next line.
sample program:
we created file name sample.java.
output:
hello world!
I am hero
my favourite food Biriyani
Java is used to the two types of comments ( // ) and (/* ------- */).
1. // I am learning java.
2./* Java is a popular programming language .It is Awesome! */.
Difference between JDK, JRE, and JVM :
Variable are containars for storing data values.
TYPES OF VARIABLE:
1.String - store text in double quotes "HELLO".
2.int -store integers (whole number),such as 123 or -123.
3.float-store floating point numbers,with decimal ,such as 19.99 0r -19.99.
4.char-strings are store single characters 'a' or 'z'.
5.boolean-store values in two states:True or False.
program:
JAVA DATETYPES:
Java datatypes is two types primitive and non-primitive.
- These are the basic built-in type provide in java.
- They store simple value directly in memory.
- Primitive datatypes such as byte, short, int, long, float, double, boolean and char.
- These are created by the programmer are java libraries that can store collection of data and complex structure..
- Also known as reference types.
- Non-Primitive datatypes such as String, Arrays and classes.
program:
- Widening Casting ( Automatic ) --> Converting a smallar type to larger type size.
byte->short->char->int->long->float->double
- Narrowing Casting ( manual ) ---> Converting a larger type to smaller type size.
double->float->long->int->char->short->byte
| Operator | Name | Description | Example |
|---|---|---|---|
| + | Addition | Adds two values together | x + y |
| - | Subtraction | Subtracts one value from another | x - y |
| * | Multiplication | Multiplies two values | x * y |
| / | Division | Divides one value by another | x / y |
| % | Modulus | Returns the division remainder | x % y |
| ++ | Increment | Increases the value of a variable by 1 | ++x |
| -- | Decrement | Decreases the value of a variable by 1 | --x |
program:
| Operator | Example | Same As | Description |
|---|---|---|---|
| = | x = 5 | x = 5 | Assign value to variable |
| += | x += 3 | x = x + 3 | Add and assign |
| -= | x -= 3 | x = x - 3 | Subtract and assign |
| *= | x *= 3 | x = x * 3 | Multiply and assign |
| /= | x /= 3 | x = x / 3 | Divide and assign |
| %= | x %= 3 | x = x % 3 | Modulus and assign |
| &= | x &= 3 | x = x & 3 | Bitwise AND and assign |
| |= | x |= 3 | x = x | 3 | Bitwise OR and assign |
| ^= | x ^= 3 | x = x ^ 3 | Bitwise XOR and assign |
| >>= | x >>= 3 | x = x >> 3 | Right Shift and assign |
| <<= | x <<= 3 | x = x << 3 | Left Shift and assign |
| >>>= | x >>>= 3 | x = x >>> 3 | Unsigned Right Shift and assign |
Java oops(object-orient programming system) concepts are the core ideas of java programming based on real-world objects.
They helps in organizing code,making it reusable, modular, and easier to maintain.
The four main OOP concept in java:-
- Declare class attribute/variable as Private.
- Provide public getter and setter methods to access and update the value of a Private variable.
- One class inherits the prorperties and methods of another class using extends -->key.
- Promote reusabilty
Five main type of inheritance:
- Single Inheritance.
- Multilevel inheritance.
- Hierarchical Inheritance.
- Multiple Inheritance.(not directly supported in java with classes)
- Hybrid Inheritance.
1.Single Inheritance.
---->One class inherits another.
program:
2.Multilevel Inheritance.
---->A class inherits from another class, which again inherits from another.
program:
Output:
---->Multiple classes inherit from a single parent.
4.Multiple Inheritance.(not directly supported in java with classes)
---->Java does not allow multiple inhertance using classes.
---->But java supports multiple inhertance using interfaces.
5.Hybrid Inheritance.
---->Java does not support hybrid inheritance with classes,but we can achieve it using interfaces.
POLYMORPHISM:
- Polymorphism means "many forms".
Two types of Polymorphism:
1.Complie-time(method overloading).
2.Run-time(method overriding).
1.Complie-time(method overloading).
Same method name , but different parameter lists.
Decided by complietime Polymorphism(early binding).
program:
output:
PS C:\Manijava> javac operation.java
PS C:\Manijava> java operation
5
6.0
- Child class provides a new implemention of a parent method.
- Decided at runtime Polymorphism(late binding).
- Hinding implementation details and showing only essitential feature.
- Achived using abstract class or interface.
- Abstract class cannot create object directly.
- Abstract method has no body,must be implemented by child.
- Syntax error (Compile-time error). ---> You assembled the bike incorrectly
- Run-time error. ---> The bike breaks while riding.
- Logical error. ---> The bike works, but you go to the wrong destination.
Mistakes in java grammar or syntax.
example:
ERROR:
2.Runtime error:
The code complies successfully , but crashes while running.
example:
Annotation:
- @entity.
- @table.
- @noargsconstructor.
- @allargsconstructor.
- @builder
- @id
- @generatedvalue
- @column
- @manytoone
- @joincolum
- @prepersist
- @restcontroller
- @Requestmapping
- @requiredargsconstructor
- @postmapping
- @requestbody
- @getmapping
- @pathvariable
- @putmapping
- @deletemapping
- @data
- @service
- @override
Spring Boot & Lombok Annotations
| Annotation | Explanation | Example |
|---|---|---|
| @Entity | Marks a Java class as a database table. JPA manages this class. | @Entity |
| @Table | Specifies the database table name. | @Table(name="products") |
| @NoArgsConstructor | Generates a constructor with no parameters. | Product p = new Product(); |
| @AllArgsConstructor | Generates a constructor with all fields. | Product p = new Product(1L,"Laptop",50000.0); |
| @Builder | Creates objects using the Builder pattern. | Product.builder().name("Laptop").price(50000).build(); |
| @Id | Marks the primary key. | @Id private Long productId; |
| @GeneratedValue | Automatically generates the primary key. | @GeneratedValue(strategy = GenerationType.IDENTITY) |
| @Column | Customizes a database column. | @Column(nullable=false) |
| @ManyToOne | Defines a many-to-one relationship. | @ManyToOne private Category category; |
| @JoinColumn | Specifies the foreign key column. | @JoinColumn(name="category_id") |
| @PrePersist | Runs before saving the entity. | @PrePersist |
| @RestController | Marks a class as a REST API controller. | @RestController |
| @RequestMapping | Defines the base URL. | @RequestMapping("/products") |
| @RequiredArgsConstructor | Generates a constructor for final fields. | @RequiredArgsConstructor |
| @PostMapping | Maps HTTP POST requests. | @PostMapping("/add") |
| @RequestBody | Converts JSON to a Java object. | public Product add(@RequestBody Product product) |
| @GetMapping | Maps HTTP GET requests. | @GetMapping("/all") |
| @PathVariable | Reads a value from the URL path. | @PathVariable Long id |
| @PutMapping | Maps HTTP PUT requests. | @PutMapping("/{id}") |
| @DeleteMapping | Maps HTTP DELETE requests. | @DeleteMapping("/{id}") |
| @Data | Generates getters, setters, equals(), hashCode(), and toString(). | @Data |
| @Service | Marks a service class for business logic. | @Service |
| @Override | Indicates a method overrides a parent method. | @Override |
Comments
Post a Comment