java-tokens-data-types
java-tokens-data-types
• Can’t be keywords
• Case-sensitive
• Begin with and consist of:
– Letters (a... Z)
– Numbers (0…9) Same as C++
– Underscore (_)
– Dollar sign ($)
Primitive Types
• boolean 8 bits (1 byte)
• char 16 bits (2 bytes)
• byte 8 bits (1 byte)
• short 16 bits (2 bytes)
• int 32 bits (4 bytes)
• long 64 bits (8 bytes)
• float 32 bits (4 bytes)
• double 64 bits (8 bytes)
1 + Addition A+B
2 – Subtraction A-B
3 * multiplication A*B
4 / Division A/B
5 % Modulus A%B
Assignment Operators in Java
• In Java programs, values for the variables are
assigned using assignment operators.
Operators Example Explanation
Simple assignment
= sum = 10 10 is assigned to variable sum
operator
+= sum += 10 This is same as sum = sum + 10
-= sum -= 10 This is same as sum = sum – 10
*= sum *= 10 This is same as sum = sum * 10
Compound assignment
/+ sum /= 10 This is same as sum = sum / 10
operators
%= sum %= 10 This is same as sum = sum % 10
&= sum&=10 This is same as sum = sum & 10
^= sum ^= 10 This is same as sum = sum ^ 10
Relational operators in Java
• Relational operators are used to find the
relation between two variables. i.e. to
compare the values of two variables in a Java
program.
S.no Operators Example Description
1 > x>y x is greater than y
2 < x<y x is less than y
3 >= x >= y x is greater than or equal to y
4 <= x <= y x is less than or equal to y
5 == x == y x is equal to y
6 != x != y x is not equal to y
Logical operators in Java
• These operators are used to perform logical
operations on the given expressions.
S.
Operators Name Example Description
no
logical
1 && (x>5)&&(y<5) It returns true when both conditions are true
AND
Syntax:
break;
Example program for break statement in Java:
Syntax : continue;
Example program for continue statement in Java:
importjava.lang.*; import java.io.*;
class constmt
{
public static void main(String args[])
{
int i;
for(i=0;i<10;i++)
{
if(i==5 || i==6)
{
System.out.println(“\n Skipping from display using continue
statement \n”+i);
continue; Output:
01234
} Skipping 5 from display using continue statement
System.out.println(““+i); Skipping 6 from display using continue statement
} 789
}
The return statement in Java
The return statement is used to explicitly
return from a method. That is, it causes
program control to transfer back to the caller
of the method.
Example program for return statement in Java
class ReturnStatement
{
public static void main(String arg[])
{
boolean t = true;
if(t)
return; // return to caller
class ForEachExample1
{
public static void main(String args[])
{
int arr[]={12,13,14,44};
Output
for(int i:arr)
:
{ 12
System.out.println(i); 13
} 14
} 44
}
Questions
1. Describe any two relational and any two logical operators in Java with
simple example.
2. Write general syntax of any two decision making statements and also
give its examples.
3. Write a program to check whether an entered number is prime or not.
4. Explain break and continue statements with example.
5. State syntax and describe working of ‘for each’ version of for loop with
one example.
6. Define a class having one 3-digit number as a data member. Initialize
and display reverse of that number.
7. Explain any four features of Java.
8. Describe ?: (Ternary operator) in Java with suitable example.
9. Explain any two bit-wise operators with example.
10. What is byte code? Explain any two tools available in JDK.
11. What is JVM? What is byte code?
12. ‘?:’ what this operator is called ? Explain with suitable example.
1. Write a program to accept a number as command line argument and print
the number is even or odd.
2. Define JDK. List the tools available in JDK explain any one in detail.
3. Explain: 1) Platform independence 2) Compiled and interpreted features of
Java.
4. Write any four mathematical functions used in Java.
5. Write a program to find sum of digits of number.
6. What do mean by typecasting? When it is needed?
7. Write a program to generate Fibonacci series : 1 1 2 3 5 8 13 21 34 55 89.
8. Write a program to print reverse of a number.
9. List eight data types available in java with their storage sizes in bytes.
10. Describe typecasting with one example.
11. What is meant by instance variable and static variable? Describe with
example.