Java Basics

Java Basics :-
---------------
Java variables
Data types


Variables:-
Variables are containers for storing data values.

In Java, there are different types of variables, for example:

String - stores text, such as "Hello". String values are surrounded by double quotes
int - stores integers (whole numbers), without decimals, such as 123 or -123
float - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
boolean - stores values with two states: true or false

Declaring Variables :-
Syntax :
type variable = value;

Data Types : -
it specify Type of data

Primitive Data Types :
A primitive data type specifies the size and type of variable values, and it has no additional methods.

There are eight primitive data types in Java:

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


Numbers
Primitive number types are divided into two groups:

Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals.
Valid types are byte, short, int and long. Which type you should use, depends on the numeric value.

Floating point types represents numbers with a fractional part, containing one or more decimals.
There are two types: float and double

User Input (Scanner) : -
The Scanner class is used to get user input.

class MyClass {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);  // Create a Scanner object
    System.out.println("Enter username");

    String userName = myObj.nextLine();  // Read user input
    System.out.println("Username is: " + userName);  // Output user input
  }
}


Methods in Scanner :-

Method Description
nextBoolean()        Reads a boolean value from the user
nextByte() Reads a byte value from the user
nextDouble()          Reads a double value from the user
nextFloat() Reads a float value from the user
nextInt()                Reads a int value from the user
nextLine() Reads a String value from the user
nextLong() Reads a long value from the user
nextShort() Reads a short value from the user


Operators:-

Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

Example
int x = 100 + 50;


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.
Ex:
int x=10;

Operator   Name               Description Example                            Try it
+ 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 from 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


Assignment Operators
---------------------
Assignment operators are used to assign values to variables.

In the example below, we use the assignment operator (=) to assign the value 10 to a variable called x:

Example
int x = 10;

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


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


Bitwise Operators: & Bitwise and Returns true if both statements are true x < 5 & x < 10 | Bitwise or Returns true if one of the statements is true x < 5 | x < 4 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) Bitwise & C1 C2 R -------- T T T T F F F T F F F F logical && C1 C2 R -------- T T T T F F F F F F ---------------------- Bitwise | C1 C2 R -------- T T T T F T F T T F F F logical || -------- T T T T F T T F F F ! (not) ------- T -> F F -> T


Logical and Bitwise.java :

package com.operator; public class LogicalAndBitwise { public static void main(String[] args){ int x=10; int y=20; System.out.println(condition1(x,10) && condition2(y,20)); // T T -> T System.out.println(condition1(x,10) && condition2(y,30)); // T F -> F System.out.println(condition1(x,20) && condition2(y,20)); // F T -> F System.out.println(condition1(x,20) && condition2(y,30)); // F F -> F /*System.out.println("-------------------------"); System.out.println(condition1(x,10) || condition2(y,20)); // T T -> T System.out.println(condition1(x,10) || condition2(y,30)); // T F -> T System.out.println(condition1(x,20) || condition2(y,20)); // F T -> T System.out.println(condition1(x,20) || condition2(y,30)); // F F -> F*/ /*int x=10; int y=20; System.out.println(x==10 & y==20); // T T -> T System.out.println(x==10 & y==30); // T F -> F System.out.println(x==20 & y==20); // F T -> F System.out.println(x==20 & y==30); // F F -> F System.out.println("-------------------------"); System.out.println(x==10 | y==20); // T T -> T System.out.println(x==10 | y==30); // T F -> T System.out.println(x==20 | y==20); // F T -> T System.out.println(x==20 | y==30); // F F -> F*/ } private static boolean condition1(int x, int value) { System.out.println("First Condition"); return x==value; } private static boolean condition2(int y, int value) { System.out.println("Second Condition"); return y==value; } }




Operators Assignments:-
1. Add two numbers
2. To add two binary numbers
3. Multiply two numbers
4. Multiply Two Floating-Point Numbers
5. Write a   program to print the result of the following operations.
Test Data:
a. -5 + 8 * 6
b. (55+9) % 9

6. Write a   program that takes two numbers as input and display the product of two numbers.
Test Data:
Input first number: 25
Input second number: 5
Expected Output :
25 x 5 = 125
7. Write a   program to print the sum (addition), multiply, subtract, divide and remainder of two numbers.
Test Data:
Input first number: 125
Input second number: 24
Expected Output :
125 + 24 = 149
125 - 24 = 101
125 x 24 = 3000
125 / 24 = 5
125 mod 24 = 5
8. Write a   program that accepts two integers from the user and then prints the sum, the difference, the product, the average, the distance (the difference between integer), the maximum (the larger of the two integers), the minimum (smaller of the two integers).
Test Data
Input 1st integer: 25
Input 2nd integer: 5
Expected Output :
Sum of two integers: 30
Difference of two integers: 20
Product of two integers: 125
Average of two integers: 15.00
Distance of two integers: 20
Max integer: 25
Min integer: 5

Comments

Popular posts from this blog

Spring Boot + Maven + RestFul + JPA + Sqlite

View & Materialized View

JDBC with Sqlite