Java is a high-level, object-oriented programming language that originally developed by Sun Microsystems and released in 1995. It was designed to be platform-independent, meaning that Java programs can run on any devices or operating systems that have a Java Virtual Machine (JVM) installed.
Java is known for its “write once, run anywhere” principle. This allows developers to write code once and run it on multiple platforms without the need for recompilation. It is achieved through the use of the JVM, which translates Java bytecode into machine-specific instructions at runtime.
The Java language is strongly typed and supports features such as automatic memory management via garbage collection, exception handling, and multithreading. It provides a rich set of libraries and APIs (Application Programming Interfaces) which make it easier for developers to build various types of applications, ranging from desktop software to web and mobile applications.
Java is used in many industries and it has a large and active developer community. It is commonly used for enterprise applications, Android app development, web development (using frameworks like JavaServer Faces, Spring, or JavaServer Pages), scientific computing, and more. Java’s popularity and versatility have contributed to its continued relevance in the software development landscape.
Add a char to a string
Compare if Strings are Equal (or Not Equal)
Comparing Two Characters
Convert an Integer to a Char
Default value of boolean primitives and Boolean objects
Exiting a Program
Get the Length of a Two-Dimensional (2D) Array
Java vs. Golang
Java vs. The .NET Framework
Print a HashMap
Print an ArrayList
Remove Quotes from a String
Java supports both single-line and multi-line comments. Single-line comments start with a //, while multi-line comments start with a /* and end with */.
// Single line comment /* Multi line comment */
Main Article: Java Comments
Printing To The Console
In Java, we can print to the console using three types of print statements:
Of these, System.out.println() is the most common because it prints and then adds a new line character so that the next print statement will print on a new line!
The print() method can be used to print without moving the cursor to a new line:
System.out.print("Hello, "); System.out.print("World!");
The println() method is almost identical, but it appends a new line as well:
System.out.println("Hello, "); System.out.println("World!");
Variables and Data Types
Variables are used to store information, and allow that information to be associated with a name.
You can declare a variable without initializing it. In this example, we are declaring an integer variable called ‘myNum’:
In this case, we haven’t yet initialized it to a value, but we could have done this in the declaration:
int myNum = 42;
In Java, a variable can be one of several data types, including Boolean (boolean), character (char), integer (int) or floating-point number (float), or string (String).
bool myBool = false; char myChar = 'A'; int myNum = 42; String myGreet = "Hello, World!";
Main Article: Java Variables
We can print variables using the print() and println() methods.
int myNum = 42; System.out.println(myNum);
We can combine variables and text inside the print statement by designating text using double quotes “” and using the ‘+‘ concatenation operator:
int myNum = 42; System.out.println("My number is " + myNum);
My number is 42
We can even do math inside a println() statement!
int x = 5; int y = 10; System.out.println(x + y);
Main Article: Printing Java Variables