Saturday, April 4, 2020

javadatatype


Data type
Data types are divided into two groups:
  • Primitive data types - includes byteshortintlongfloatdoubleboolean and char
  • Non-primitive data types - such as StringArrays and Classes
Data Type
Size
Description
byte
1 byte
Stores whole numbers from -128 to 127
short
2 bytes
Stores whole numbers from -32,768 to 32,767
int
4 bytes
Stores whole numbers from -2,147,483,648 to 2,147,483,647
long
8 bytes
Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float
4 bytes
Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double
8 bytes
Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean
1 bit
Stores true or false values
char
2 bytes
Stores a single character/letter or ASCII values

Java divides the operators into the following groups:
  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Bitwise operators

Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
Operator
Name
Description
Example
+
Addition
Adds together two values
x + y
-
Subtraction
Subtracts one value from another
x - y
*
Multiplication
Multiplies two values
x * y
/
Division
Divides one value by another
x / y
%
Modulus
Returns the division remainder
x % y
++
Increment
Increases the value of a variable by 1
++x

--
Decrement
Decreases the value of a variable by 1
--x

Java Assignment Operators

A list of all assignment operators:
Operator
Example
Same As
=
x = 5
x = 5
+=
x += 3
x = x + 3
-=
x -= 3
x = x - 3

*=
x *= 3
x = x * 3
/=
x /= 3
x = x / 3
%=
x %= 3
x = x % 3
&=
x &= 3
x = x & 3
|=
x |= 3
x = x | 3
^=
x ^= 3
x = x ^ 3
>>=
x >>= 3
x = x >> 3
<<=
x <<= 3
x = x << 3

Java Comparison Operators
Comparison operators are used to compare two values:
Operator
Name
Example
==
Equal to
x == y
!=
Not equal
x != y

> 
Greater than
x > y
< 
Less than
x < y
>=
Greater than or equal to
x >= y
<=
Less than or equal to
x <= y

Java Logical Operators
Logical operators are used to determine the logic between variables or values:
Operator
Name
Description
Example
&& 
Logical and
Returns true if both statements are true
x < 5 &&  x < 10
|| 
Logical or
Returns true if one of the statements is true
x < 5 || x < 4

!
Logical not
Reverse the result, returns false if the result is true
!(x < 5 && x < 10)
  • In Java, every application begins with a class name, and that class must match the filename.
Let's create our first Java file, called MyClass.java, which can be done in any text editor (like Notepad).
The file should contain a "Hello World" message, which is written with the following code:
MyClass.java
public class MyClass {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}
Save the code in Notepad as "MyClass.java". Open Command Prompt (cmd.exe), navigate to the directory where you saved your file, and type "javac MyClass.java":
C:\Users\Your Name>javac MyClass.java
C:\Users\Your Name>java MyClass
The output should read:
Hello World

 

Q. Write a program in JAVA two add two numbers given by users?

import java.util.Scanner; // Import the Scanner class
class MyClass {
  public static void main(String[] args) {
    int x, y, sum;
    Scanner myObj = new Scanner(System.in); // Create a Scanner object
    System.out.println("Enter a number:");
    x = myObj.nextInt(); // Read user input
    System.out.println("Enter another number:");
    y = myObj.nextInt(); // Read user input
    sum = x + y;  // Calculate the sum of x + y
    System.out.println("Sum is: " + sum); // Print the sum
  }
}
Control Statement in java
  1. If……else
  2. For……loop
  3. While….loop
  4. Do……while
  5. Switch….case
  6. Break
  7. Continue
  8. Goto

The if Statement

Use the if statement to specify a block of Java code to be executed if a condition is true.
if (condition) {
  // block of code to be executed if the condition is true
} else {
  // block of code to be executed if the condition is false
}
Example
int x = 20;
int y = 18;
if (x > y) {
  System.out.println("x is greater than y");
}


1 comment: