Java Variables, Data Types, and Operators Explained

🔹 Variables in Java : A variable is a container that holds data which can be changed during program execution. Types of variables : 1) Local variable 2) Static variable 3) Instance variable Syntax: datatype variableName = value; Example : int age = 20; String name = "Guru"; double salary = 45000.50; 👉 Rules for variables: - Must start with a letter, $, or _ (not a digit). - Cannot use keywords (like class, int). - Java is case-sensitive (Age and age are different). 🔹 Data Types in Java : Java is a statically-typed language, so each variable must have a data type. 1. Primitive Data Types (8 types) : 👉 Data Type and Size : byte = 1 byte short = 2 byte int = 4 byte long = 8 byte float = 4 byte double = 8 byte char = 2 byte boolean = 1 bit 2. Non-Primitive Data Types : - String → "Hello Java" - Arrays → int[] arr = {1,2,3}; - Classes & Objects 🔹 Operators in Java : Operators are special symbols that perform operations on variables and values. 1. Arithmetic Operators : + Addition - Subtraction * Multiplication / Division % Modulus (remainder) 2. Relational (Comparison) Operators : == Equal to != Not equal to > Greater than < Less than >= Greater than or equal <= Less than or equal 3. Logical Operators : && Logical AND || Logical OR ! Logical NOT 4. Assignment Operators : = Assign += Add and assign -= Subtract and assign *= Multiply and assign /= Divide and assign %= Modulus and asshshift 5. Unary Operators : ++ Increment -- Decrement + Positive - Negative ! Logical NOT 6. Conditional (Ternary) Operator : variable = (condition) ? value_if_true : value_if_false; 7. Bitwise Operators : & AND | OR ^ XOR ~ NOT << Left shift >> Right shift >>> Unsigned right shift #Corejava #Java #learningjava #Programming #Coding #Fortunecloud #Cravita

  • diagram

To view or add a comment, sign in

Explore content categories