lec3-Java Tutorial
lec3-Java Tutorial
Data Types
Data types in Java fall under the following categories
Keywords
Words that are reserved and used by the Java compiler. They cannot be
used as an Identifier.
{You can visit docs.oracle.com for a comprehensive list}
Code as Described in the Video
package com.company;
boolean a = true;
System.out.print(age);
String str = "Harry";
System.out.println(str);
}
}
Java Tutorial: Getting User Input
in Java
Lec5
Reading data from the Keyboard :
Scanner class of java.util package is used to take input from the user's
keyboard.The Scanner class has many methods for taking input from the
user depending upon the type of input. To use any of the methods of the
Scanner class, first, we need to create an object of the Scanner class as
shown in the below example :
Copy
Taking an integer input from the keyboard :
Scanner S = new Scanner(System.in); //(Read from the
keyboard)
int a = S.nextInt(); //(Method to read from the keyboard)
Copy
Code as Described in the Video
package com.company;
import java.util.Scanner;
}
}
import java.util.Scanner;
// Question2
// float subject1 = 45;
// float subject2 = 95;
// float subject3 = 48;
// float cgpa = (subject1 + subject2 +subject3)/30;
// System.out.println(cgpa);
// Question 3
// System.out.println("What is your name");
// Scanner sc = new Scanner(System.in);
// String name = sc.next();
// System.out.println("Hello " + name + " have a good
day!");
// Question 5
System.out.println("Enter your number");
Scanner sc = new Scanner(System.in);
System.out.println(sc.hasNextInt());
}
Types of operators :
1. Arithmetic Operators :
Arithmetic operators are used to perform mathematical
operations such as addition, division, etc on expressions.
Arithmetic operators cannot work with Booleans.
% operator can work on floats and doubles.
Let x=7 and y=2
*
Used to multiply two values. x * y = 14
(Multiplication)
2. Comparison Operators :
As the name suggests, these operators are used to compare
two operands.
Let x=7 and y=2
3. Logical Operators:
These operators determine the logic in an expression
containing two or more values or variables.
Let x = 8 and y =2
&& (logical Returns true if both operands are true. x<y && x!=y -->
and) True
|| (logical or) Returns true if any of the operand is true. x<y && x==y --> True
Returns true if the result of the expression is false and !(x<y && x==y)
! (logical not)
vice-versa --> False
4. Bitwise Operators :
These operators perform the operations on every bit of a
number.
Let x =2 and y=3. So 2 in binary is 100, and 3 is 011.
& (bitwise and) 1&1 =1, 0&1=0,1&0=0,1&1=1, 0&0 =0 (A & B) = (100 & 011) = 000
Precedence of operators
The operators are applied and evaluated based on precedence. For
example, (+, -) has less precedence compared to (*, /). Hence * and / are
evaluated first.
In case we like to change this order, we use parenthesis ().
Code as Described in the Video
package com.company;
// 2. Assignment Operators
int b = 9;
b *= 3;
System.out.println(b);
// 3. Comparison Operators
// System.out.println(64<6);
// 4. Logical Operators
// System.out.println(64>5 && 64>98);
System.out.println(64>5 || 64>98);
// 5. Bitwise Operators
System.out.println(2&3);
// 10
// 11
// ----
// 10
}
}
package com.company;
public class cwh_09_ch2_op_pre {
public static void main(String[] args) {
// Precedence & Associativity
//int a = 6*5-34/2;
/*
Highest precedence goes to * and /. They are then
evaluated on the basis
of left to right associativity
=30-34/2
=30-17
=13
*/
//int b = 60/5-34*2;
/*
= 12-34*2
=12-68
=-56
*/
//System.out.println(a);
//System.out.println(b);
// Quick Quiz
int x =6;
int y = 1;
// int k = x * y/2;
int b = 0;
int c = 0;
int a = 10;
int k = b*b - (4*a*c)/(2*a);
System.out.println(k);
}
}
1. int y=7;
2. int x = ++y*8;
3. value of x?
4. char a = ‘B’;
5. a++; (a is not ‘C’)