Java Codes Basic to Advance
2.03K subscribers
4 photos
2 videos
1 file
48 links
Download Telegram
//10 Syntax:OOPS:Class Constructors
public class Main {
public static void main(String[] args) {
Main myCar = new Main(1969, "Mustang");
System.out.println(myCar.modelYear + " " + myCar.modelName);
}

int modelYear;
String modelName;

public Main(int year, String name) {
modelYear = year;
modelName = name;
}

}
//10 Syntax:OOPS:Encapsulation
public class Person {
public static void main(String[] args) {
Person myObj = new Person();
myObj.setName("John"); // Set the value of the name variable to "John"
System.out.println(myObj.getName());
}

private String name; // private = restricted access
// Getter
public String getName() {
return name;
}
// Setter
public void setName(String newName) {
this.name = newName;
}
}
//11 Syntax:Enums
public class Main {
public static void main(String[] args) {
Level myVar = Level.Mid;
System.out.println(myVar);
}

enum Level {
Max,
Mid,
Min
}
}
//12 Syntax:user input
import java.util.Scanner;

class Main {
public static void main(String[] args) {
Scanner Obj = new Scanner(System.in);

System.out.println("Enter name, age and salary:");
String name = Obj.nextLine();
int age = Obj.nextInt();
double salary = Obj.nextDouble();
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
}
👍1
//13 Syntax:Exception handling
public class Main {
public static void main(String[ ] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Oh No...");
}
}
}
//13 Syntax:Exception handling 02
public class Main {
public static void main(String[ ] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Oh No...");
} finally {
System.out.println("I will run whatever happen.");
}
}
}
👍1
//13 Syntax:Exception handling 03
public class Main {
public static void main(String[ ] args) {
throw new ArithmeticException("Access denied ");
}
//No Output only a error Msg will be shown
}
Exception in thread "main" java.lang.ArithmeticException: Access denied at Main.main(Main.java:3)

Output of 13 Syntax:Exception handling 03
👍1
//14 Syntax:File Create / write
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class CreateFile {
public static void main(String[] args) {
try {
File Obj = new File("Give Location where to create File");
if (Obj.createNewFile()) {
System.out.println("File created: " + Obj.getName());
FileWriter MWriter = new FileWriter("give Location of file which to write");
MWriter.write("What am I writting");
MWriter.close();
System.out.println("Successfully wrote to the file.");
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
//Note This code can only run in Your PC
1👍1
Output:
File created: test.txt
Successfully wrote to the file

Output of 14 Syntax:File Create / write
//14 Syntax:File Read / Info
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ReadFile {
public static void main(String[] args) {
File Obj = new File("Location of text File");
if (Obj.exists()) {
System.out.println("File name: " + Obj.getName());
System.out.println("Absolute path: " + Obj.getAbsolutePath());
System.out.println("Writeable: " + Obj.canWrite());
System.out.println("Readable " + Obj.canRead());
System.out.println("File size in bytes " + Obj.length());
}
else {
System.out.println("The file does not exist.");
}
try {
File ObjR = new File("Location of text file");
Scanner MReader = new Scanner(ObjR);
while (MReader.hasNextLine()) {
String data = MReader.nextLine();
System.out.println(data);
}
MReader.close();
}catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
//Note This code can only run in Your PC
👍2
Output:
File name: test.txt
Absolute path: /home/runner/compilers/./test/test.txt
Writeable: true
Readable true
File size in bytes 18
What am I writting

Output of 14 Syntax:File Read / Info
//14 Syntax:File Delete and folder
import java.io.File;

public class DeleteFile {
public static void main(String[] args) {
try{
File DObj = new File("Location Of File");
if (DObj.delete()) {
System.out.println("Deleted the file: " + DObj.getName());
} else {
System.out.println("Failed to delete the file.");
}
}catch(Exception e){
System.out.println("Failed to delete the folder.");
}
finally{
File DFObj = new File("Location of Folder");
if (DFObj.delete()) {
System.out.println("Deleted the folder: " + DFObj.getName());
} else {
System.out.println("Failed to delete the folder.");
}
}
}
}
//Note This code can only run in Your PC
👍4
Output:
Deleted the file: test.txt
Failed to delete the folder.

Output of 14 Syntax:File Delete and folder
Remaining Topics like Inheritance, Polymorphism, Hash Map, Wrapper Classes, Regex, Threads, Lamda will be added soon
👍1
Java all Codes listed see pin message 👆

Or you can search any keyword

Example: "function" to see function related codes
#Taskforyou

Write a Java program that remove the extra spaces from the following string:

' I am learning Java object oriented programming '
👍1
Write code on task's comment, which code will be best that code should be posted here with those name

After 8pm today
👍1