How to Use the Java Compiler
To use the Java Compiler, follow these steps:
- In the code editor, write your Java code.
- Click the "RUN" button to compile and run your code.
- The output will be displayed in the console beside the code editor.
Taking Inputs
In Java, you can take inputs from the user in various ways. Here are some examples:
String Input
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a string:");
String input = scanner.nextLine();
System.out.println("You entered: " + input);
}
}
Array of Numbers
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter numbers separated by spaces:");
String[] numbers = scanner.nextLine().split(" ");
System.out.println("Array of numbers: ");
for (String number : numbers) {
System.out.println(number);
}
}
}
Matrix Input
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of rows:");
int rows = scanner.nextInt();
int[][] matrix = new int[rows][];
for (int i = 0; i < rows; i++) {
System.out.println("Enter row " + (i + 1) + " separated by spaces:");
String[] row = scanner.nextLine().split(" ");
matrix[i] = new int[row.length];
for (int j = 0; j < row.length; j++) {
matrix[i][j] = Integer.parseInt(row[j]);
}
}
System.out.println("Matrix: ");
for (int[] row : matrix) {
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
}
}
Importing Libraries
Java has a rich set of built-in libraries that can be imported and used in your programs. Here are some examples:
Using the Math Library
public class Main {
public static void main(String[] args) {
double result = Math.sqrt(25);
System.out.println("Square root of 25 is " + result);
}
}
Using the Arrays Library
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = {5, 2, 8, 7, 1};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));
}
}
Using the ArrayList Class
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
System.out.println(list);
}
}
Syntax 101
Java is a statically-typed, object-oriented programming language. Here's a primer on the major syntax basics of Java:
Variables
Variables in Java must be declared before they can be used. You must also specify the type of data the variable can hold.
int age = 25; // Integer
double pi = 3.14; // Double
boolean isAdult = true; // Boolean
char
initial = 'J'; // Character
String name = "John Doe"; // String
Arrays
Arrays in Java are used to store multiple values in a single variable.
int[] numbers = {1, 2, 3, 4, 5}; // Array of integers
Control Structures
Java includes control structures such as if
, else
, for
, while
, and switch
.
// If-Else
if (age > 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
// For loop
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
// While loop
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
// Switch
switch (day) {
case "Monday":
System.out.println("Start of the work week");
break;
case "Friday":
System.out.println("End of the work week");
break;
default:
System.out.println("Midweek");
}
Functions
Functions in Java are declared within a class. They are defined with the void
keyword if they do not return a value.
void greet() {
System.out.println("Hello, world!");
}
greet(); // Calls the function
Object-Oriented Programming (OOP)
Java is an object-oriented programming language. This means that it focuses on objects and classes.
class Car {
String brand; // Field
int year; // Field
Car(String brand, int year) { // Constructor
this.brand = brand;
this.year = year;
}
void start() { // Method
System.out.println("Car is starting.");
}
}
Car myCar = new Car("Toyota", 2005); // Create an object of the Car class
myCar.start(); // Call the start method
Java Online Test
A Java online test is an effective way to assess an individual's Java programming skills. These tests typically include a mix of theoretical questions and practical coding challenges. By attempting these tests, candidates can demonstrate their understanding of Java concepts, their problem-solving abilities, and their proficiency in writing efficient code. Java online tests are commonly used in technical interviews, coding bootcamps, and online learning platforms to gauge a learner's understanding and proficiency in Java.