Java is a popular, high-level programming language known for its portability, performance, and robust nature. This README provides a comprehensive guide to the basics of Java programming, including input/output statements and fundamental syntax.
- Introduction
- Basic Syntax
- Data Types and Variables
- Input/Output Statements
- Control Flow Statements
- Example Programs
- Further Reading and Resources
- Contact Information
Java is a versatile and widely-used programming language that follows the object-oriented programming paradigm. It is designed to be platform-independent, allowing code to run on any machine equipped with the Java Virtual Machine (JVM).
The simplest Java program is the "Hello World" program. This program prints "Hello, World!" to the console.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Comments are used to explain code and are ignored by the compiler.
- Single-line comments start with
//
.
// This is a single-line comment
- Multi-line comments are enclosed within
/*
and*/
.
/*
This is a multi-line comment.
It can span multiple lines.
*/
Java supports various data types, which are categorized into two groups: primitive and reference types.
int
: integerfloat
: floating-point numberdouble
: double-precision floating-point numberchar
: single characterboolean
: true or falsebyte
,short
,long
: other integer types
Variables are used to store data. Here's how to declare and initialize variables:
int number = 5;
float decimal = 3.14f;
char letter = 'A';
boolean flag = true;
To display output to the console, use System.out.print()
and System.out.println()
.
System.out.print()
: Prints text without a newline at the end.
System.out.print("Hello, ");
System.out.print("World!");
System.out.println()
: Prints text with a newline at the end.
System.out.println("Hello, World!");
To take input from the user, use the Scanner
class from the java.util
package.
- Import the
Scanner
class:
import java.util.Scanner;
- Create a
Scanner
object:
Scanner scanner = new Scanner(System.in);
- Use the
Scanner
methods to read input:
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your age: ");
int age = scanner.nextInt();
Java provides several control flow statements for decision making, looping, and branching.
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Other day");
break;
}
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
import java.util.Scanner;
public class AddTwoNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter second number: ");
int num2 = scanner.nextInt();
int sum = num1 + num2;
System.out.println("The sum is: " + sum);
}
}
import java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (num % 2 == 0) {
System.out.println(num + " is even.");
} else {
System.out.println(num + " is odd.");
}
}
}
For questions or support, please contact:
- Email: atharvkote3@gmail.com