Posts

New Post

Java Expressions, Statements and Blocks

 In the previous Post, we have used expressions, statements and blocks without explaining them too much. Now that you know variables, operators, and literals, it will be easier to understand these concepts. Java expressions A Java expression consists of variables, operators, literals, and method calls. To learn more about method calls, visit Java Methods. For example, int score; score = 90 ; Here,  score = 90  is an expression that returns an  int . Take another example, Double a = 2.2 , b = 3.4 , result; result = a + b - 3.4 ; Here  a + b - 3.4  is an expression. if (number1 == number2) System.out.println( "Number 1 is larger than number 2" ); Here,  number1 == number2  is an expression that returns a Boolean value. Likewise,  "Number 1 is larger than number 2"  is a string expression. Java Statements In Java, each instruction is a complete unit of execution. For example, int score = 9 * 5 ; Here we have a statement. The complet...

Java Basic Input and Output

 Java output In Java you can just use System.out.println(); or System.out.print(); or System.out.printf(); to send the output to the standard output (screen). Here, The  System  is a class out  is a  public   static  field: it accepts output data. Don't worry if you don't understand it. We will discuss the  class ,  public  and  static  in the following chapters. Let's take an example to pull out a line. class AssignmentOperator { public static void main (String[] args) { System.out.println( "Java programming is interesting." ); } } Output: Java programming is interesting. Here we have used the  println()  method to display the string. Difference between println (), print () and printf () print()  - It prints a string inside the quotes. println()  - It prints a string inside quotes similar to the  print()  method. Then the cursor moves to the start of the next line....

Java operators

Operators are symbols that perform operations on variables and values. For example,  +  is an operator used for addition, while  *  is also an operator used for multiplication. Operators in Java can be classified into 5 types: Arithmetic operators Assignment operators Relational operators Logical operators Unary operators Bitwise operators 1. Java arithmetic operators Arithmetic operators are used to perform arithmetic operations on variables and data. For example, a + b;   Here the  +  operator is used to add two variables  a  and  b . Likewise, there are various other arithmetic operators in Java. Operator Operation + Addition - Subtraction * Multiplication / Division % Modulo Operation (Remainder after division) Example 1: Arithmetic operators class Main { public static void main (String[] args) { // declare variables int a = 12 , b = 5 ; // addition operator System.out.println( "a + b = " + (a + b));...