***** _What is Java?****_
Java is a popular programming language, created in 1995.It is owned by Oracle, and more than 3 billion devices run Java.
It is used for:
- Mobile applications (specially Android apps)
- Desktop applications
- Web applications
- Web servers and application servers Games 5.Database connection
Java Syntax
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Java Comments
*Single-line comments start with two forward slashes (//).
Any text between // and the end of the line is ignored by Java (will not be executed).
Multi-line comments start with / and ends with */.
_Java Variables_
In Java, there are different types of variables, for example:
String - stores text, such as "Hello". String values are surrounded by double quotes.
2_. int_ - stores integers (whole numbers), without decimals, such as 123 or -123.float - stores floating point numbers, with decimals, such as 19.99 or -19.99.
4._ char_ - stores single characters, such as 'a' or 'B'. Char
values are surrounded by single quotes.
- boolean - stores values with two states: true or false.
**_Declaring (Creating) Variables: syntax :-
type variableName = value;
Java Data Types
Data types are divided into two groups:
Primitive data types - includes byte, short, int, long, float, double, boolean and char.
Non-primitive data types - such as String, Arrays and Classes.
Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127.
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values.
Java Type Casting
Type casting is when you assign a value of one primitive data type to another type.
In Java, there are two types of casting:
1.Widening Casting (automatically) - converting a smaller type to a larger type size
byte -> short -> char -> int -> long -> float -> double
2.Narrowing Casting (manually) - converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
5_. boolean -** stores values with two states: true or false.
Top comments (0)