# Output & Basic Data Types ## Basic Operators and Operands: Expression: ```java Line 1: 10 + 20 Line 2: 15 * 2 ``` Let us try to observe operands and operator in the above lines: **Line 1:** Operator : **+** Operands : **10 and 20 both are operands** **Line 2:** Operator : __*__ Operands : **15 and 2 both are operands** ## Numbers There are two types of numbers: * __Decimal :__ The numbers having decimal point. * Ex : 12.54 , -20.0, 6.97, 0.0 * __Integers :__ The numbers without decimal point. * Ex : 12, -17, 54, 0, -5 ## Some syntax to keep in mind: ### Syntax 1: When we divide ( / ) integers in Java, we only get quotient. Code : ```java Line 1: System.out.println(15/4); ``` Output : ``` java 3 ``` Explanation : ```plaintext= In calculator, 15/4 = 3.75 But in Java, we only get 3 ``` ### Syntax 2 : Dividing integers by 0 is not allowed, we get error. Code : ```java Line 1: System.out.println(15/0); ``` Output : ``` java [NonZeroExitCode] Your code encountered a runtime error Exception in thread "main" java.lang.ArithmeticException: / by zero ``` Explanation : ```plaintext= Dividing integers by 0 giver error in Java ``` ### Expression evaluation ```java Line 1: System.out.println(6*7/6); ``` Output : ``` java 7 ``` Explanation : ```plaintext= Here, 6*7 gives 42 and 42/6 = 7 ``` ### Syntax 3 : Priority in Basic Operators 1. Rank 1 : **()** 2. Rank 2 : __*__ , __/__ 3. Rank 3 : __+__ , __-__ ### Few important rules of doing operations #### Rule 1 : If we have same priority operators, whichever comes first from left to right that will be evaluated first. Code : ```java Line 1: System.out.println(15/5*4); ``` Output : ``` java 12 ``` Explanation : ```plaintext= Here, * and / both have same priority. Because / comes first from left to right we will evaluate 15/5 first. 15/5*4 => 3 * 4 => 12 ``` #### Rule 2 : If we have different priority operators, whichever has highest priority that will be evaluated first. Code : ```java Line 1: System.out.println(5 + 2 * 3); ``` Output : ``` java 11 ``` Explanation : ```plaintext= Here, * has higher priority. 2*3 will be evaluated first. 5 + 2 * 3 => 5 + 6 => 11 ``` #### Rule 3 : With '+' operator, If one Operand is text then we concatenate both Operands Code : ```java Line 1: System.out.println("Hello" + "World"); ``` Output : ``` java HelloWorld ``` Explanation : ```plaintext= Since, there is a + operator with text, we are concatenating both operands. ``` Code : ```java Line 1: System.out.println("Hello" + 2); ``` Output : ``` java Hello2 ``` Explanation : ```plaintext= Since, there is a + operator with text, we are concatenating both operands. ``` #### Rule 4 : With text operand, only '+' operator can be used. Code : ```java Line 1: System.out.println("Hello" * 3); ``` Output : ``` java Error : bad operand types ``` Explanation : ```plaintext= With text operand, * cannot be used ``` Code : ```java Line 1: System.out.println("WelcomeHome" - "Home"); ``` Output : ``` java Error : bad operand types ``` Explanation : ```plaintext= With text operand, - cannot be used ``` ## Creating a variable Syntax: ```java Line 1 : type name; Line 2 : name = value; OR Line 3 : type name = value; ``` Explanation: ```plaintext= Line 1 is the declaration step and Line 2 is initialisation. In Line 3, we are doing both declaration adn initialisation step together. ``` #### Syntax 1: If we want to store a integer number, type is int Code: ```java Line 1: int y; Line 2: y = 30; Line 3: int x = 20; ``` Explanation: ```plaintext= Line 1 is creating a variable(container) of type 'int' and name 'y' Line 2 is storing the value '30' in the variable y Line 3 is creating a variable of type 'int' and name 'x' and storing value '20' in the variable x ``` #### Syntax 2: When we write variable name the value of the variable is used. Code: ```java Line 1: int y = 30; Line 2: System.out.println(y); Line 3: int x = y+10; Line 4: System.out.println(x); ``` Output: ```java 30 40 ``` Explanation: ```plaintext= In Line 1, a variable is created : type : int name : y value : 30 In Line 2, we are printing y, so the value of this variable will be printed which is 30. In Line 3, a variable is created: type : int name : x value : y + 10 => 30 + 10 = 40 In Line 4, we are printing x, so the value of this variable will be printed which is 40. ```