Understanding the Basics of Java Syntax and Data Types
Understanding the Basics of Java Syntax and Data Types
Introduction
• Java is a high-level, object-oriented programming language developed by Sun
Microsystems (now owned by Oracle)
• It is widely used for building secure and robust applications
• Java is platform-independent and can run on various operating systems
Data Types
o Java has eight primitive data types:
• byte: 8-bit signed integer
• short: 16-bit signed integer
• int: 32-bit signed integer
• long: 64-bit signed integer
• float: 32-bit single-precision floating-point number
• double: 64-bit double-precision floating-point number
• boolean: represents true or false value
• char: 16-bit Unicode character
Non-primitive data types, also known as reference types, include:
• Classes
• Interfaces
• Arrays
Variables
• In Java, we use variables to store data
• A variable must have a data type and a name
• To declare a variable in Java, we use the following syntax: dataType variableName;
• To initialize a variable, we use the assignment operator = like this: variableName = value;
Operators
Java has several types of operators, including:
• Arithmetic operators: +, -, *, /, %
• Comparison operators: ==, !=, >, <, >=, <=
• Logical operators: &&, ||, !
• Assignment operators: =, +=, -=, *=, /=, %=
• Parentheses can be used to change the order of operations.
Control Statements
• Java has various control statements, including:
• if-else statements: used for decision making
• switch-case statements: used for multiple choices
• for loops: used for repetition with a counter
• while loops: used for repetition until a condition is met
• do-while loops: used for repetition at least once
Reading and Writing to Console
• Java provides System.out.println() method for writing to console
• To read user input from console, we can use Scanner class like this:
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
That's it for the basics of Java syntax and data types with this knowledge, you can start writing
simple Java programs and gradually move on to more complex ones.