Java is a widely-used, object-oriented programming language known for its portability, performance, and security. It follows the principle of "Write Once, Run Anywhere" (WORA), meaning that code written in Java can run on any platform that supports Java without needing recompilation.
Java was developed by Sun Microsystems in 1995. It is used for building cross-platform applications, from web apps to mobile apps and large-scale enterprise systems.
Java syntax is similar to other C-based languages but with some unique features. Here are some basic elements of Java syntax:
main
method is the entry point of any Java application. It must be declared aspublic static void main(String[] args)
.//
for single-line comments and/* ... */
for multi-line comments.public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Variables store data values, and Java is a statically-typed language, meaning you must declare the type of a variable. Common data types include:
int
: Integer numbers (e.g.,int age = 30;
)double
: Floating-point numbers (e.g.,double price = 19.99;
)char
: Single characters (e.g.,char grade = 'A';
)boolean
: True or false values (e.g.,boolean isJavaFun = true;
)String
: Sequence of characters (e.g.,String name = "John";
)Arrays
: Collections of elements (e.g.,int[] numbers = {1, 2, 3};
)Java operators are used to perform operations on variables and values. Common operators include:
+
,-
,*
,/
,%
==
,!=
,>
,<
,>=
,<=
&&
(AND),||
(OR),!
(NOT)=
,+=
,-=
,*=
,/=
Control flow statements determine the direction of execution of code. Common control flow statements include:
if
: Executes a block of code if its condition is true.else
: Executes a block of code if theif
condition is false.switch
: Allows multi-way branching based on the value of a variable.for
: Repeats a block of code a specific number of times.while
: Repeats a block of code as long as its condition is true.do-while
: Repeats a block of code at least once and then continues based on a condition.Methods are blocks of code that perform a specific task and can be called to execute. They help to organize and reuse code.
public class Calculator {
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(5, 3);
System.out.println("Sum: " + result);
}
}
Java is an object-oriented language, and it includes several key concepts:
Exception handling allows a program to handle runtime errors gracefully. Java usestry
,catch
, andfinally
blocks for exception handling.
try {
// Code that may throw an exception
} catch (ExceptionType e) {
// Code to handle the exception
} finally {
// Code that will always execute
}
Java provides several classes for handling input and output. Common classes include:
I think you are here to get to know me.
🌐 Connect with me across platforms.
🤝 Hit me up on these links, and let's turn ideas into action!