Java Basic Concepts

Java-Write once , run anywhere!

Java is one of the most popular programming languages. You can use Java to build Android apps, games,banking applications, web apps and much more!

Java’s slogan is “Write once, run anywhere”. This means that the same Java code can run on different platforms, including mobile, desktop and other portable systems. #JAVA IS PLATFORM INDEPENDENT

Statements

In programming , a statement is a single line of code that performs a specific task. So, the example code that outputs the slogan is a statement.

��Each statement needs to end with a semicolon(;)

System.out.println("Hello World!");

NOTE: This instruction sends a line of text to the screen.

Variables

Variables store data for processing.

A variable is given a name (or identifier), such as area, age, height, and the like. The name uniquely identifies each variable, assigning a value to the variable and retrieving the value stored.

Variables have types. Some examples:

- int: for integers (whole numbers) such as 123 and -456

- double: for floating-point or real numbers with optional decimal points and fractional parts in fixed or scientific notations, such as 3.1416, -55.66.

- String: for texts such as "Hello" or "Good Morning!". Text strings are enclosed within double quotes.

String name="Edward";

This creates a variable called name of type String,and assigns it the value.

class MyClass {
public static void main(String[] args) {
String name ="David";
int age = 42;
double score =15.9;
char group = 'Z';
}
}

char stands for character and holds a single character.

Another type is the Boolean type, which has only two possible values: true and false. This data type is used for simple flags that track true/false conditions. For example:

boolean online = true;

Comments

The purpose of including comments in your code is to explain what the code is doing. Java supports both single and multi-line comments. All characters that appear within a comment are ignored by the Java compiler.

A single-line comment starts with two forward slashes and continues until it reaches the end of the line.

// this is a single-line comment
x = 5; // a single-line comment after code

NOTE: Adding comments as you write code is a good practice, because they provide clarification and understanding when you need to refer back to it, as well as for others who might need to read it.

Multi-line Comments

Java also supports comments that span multiple lines. You start this type of comment with a forward slash followed by an asterisk, and end it with an asterisk followed by a forward slash.

/* This is also a
comment spanning
multiple lines */

Note that Java does not support nested multi-line comments. However, you can nest single-line comments within multi-line comments.