top
April flash sale

Search

Java Tutorial

Data Types in Java There are 2 types of data: Primitive: The primitive data typesinclude boolean, char, byte, short, int, long, float and double. Non-primitive: The non-primitive data types include classes, interfaces, and arrays. Numbers in java Number is the most primitive way of storing information in any computer language. You can store a number in a variable, but distinct formats are used to represent a number, and a separate quantity of storage is required for each format. The easiest format records a number that does not have a fractional part, i.e. it uses integers. You can choose to use one byte, two, four or eight bytes when you store an integer. The lower the amount of storage you use, obviously, the lower the range of numbers you can use. The Java integer data types are below: byte: It can store 1 byte and ranges -128 to 127 short: It can store 2 bytes and ranges -32,768 to 32,767 int: It can store  4 bytes and ranges -2,147,483,648 to 2,147,483,647 long: It can store 8 bytes and ranges -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Floating numbers in java You will now want to use numeric values with fractional parts, of course.. For reasons we don't have to go into, the format most used to store fractional numbers is called "floating point." A floating point variable may store a fractional number value, but you need to be conscious of the range that can be stored and the accuracy. Java has two kinds of floating points and it is hard to try to pin down the accuracy and range they can represent. Rather, the approximate amount of accuracy numbers they provide is easier to think about: float: It can store 4 bytes and up to 7 decimal digits double: It can store 8 bytes and up to16 decimal digits Characters In Java, the data type to store characters is char. Characters are represented using Unicode. Unicode is an international character set representing all the characters. In Java language, char ranges from 0 to 65,535. Characters are always written within ' '.For example: char first = 'f';  Stringclass in java String is a series of characters because, for example, "Name" is a 4 character string. String is an immutable object in java which means it is constant and cannot be altered once it is formed. There are basically 2 ways to create a String object: 1. By string literal: Double quotes are used to create Java String literal i.e. “Country” The JVM checks the "string pool" first every time you generate a string literal. If the string is already in the pool, a reference is returned to the pooled instance. If the string does not exist in the pool, it creates and places a new string instance in the pool. 2. By new keyword: String s=new String("Country");//creates two objects and one reference variable In the above case, in normal (non-pool) heap memory, JVM will create a new string object, and the literal "Country" will be placed in the constant string pool. In a heap (non-pool), the variable s refers to the object. Unicode in java Unicode is a normal encoding 16-bit character and can represent nearly every character of the world's well-known languages. There were numerous standards for character encoding before Unicode: KOI-8: It was used for Russian. ASCII: It was used in the United States. ISO 8859-1: It was used for the Western European Language. GB18030 & BIG-5: It was used for Chinese. Some characters used a single byte, some two, to help multinational application codes. The same code may even represent a different character in one language and may represent other characters in another language The unicode scheme was created to solve the above shortcomings where each character is represented by 2 bytes. Strictfp keyword in java Strictfp is a Java programming language modifier that restricts the calculation of floating points to guarantee portability. The Java virtual machine (JVM) version 1.2 introduced the strictfp command into Java and is available for use on all currently updated Java VMs. Important points: When a class or interface with a strtfp modifier is declared, then all methods declared in the class / interface and all nested types declared in the class are strictfp implicitly. Only classes, interfaces and methods are used with the strictfp modifier. Unable to use strictfp with abstract methods. However, abstract classes / interfaces can be used. Since interface methods are implicitly abstract, within an interface strtfp cannot be used with any methods. Operators in java Java provides many types of operators which can be used according to the need. They are classified based on the functionality they provide. Some of the types are: Arithmetic Operators:They are used on primitive data types to conduct easy arithmetic operations. % : Formodulo + : Foraddition * :  Formultiplication / : Fordivision – : Forsubtraction Assignment Operator : Assignment Operator i.e. ‘=’ is used to assign any variable to a value. It has a right-to-left associativity, i.e. the right-hand value of the operator is assigned to the left-hand variable and therefore the right-hand side value must be declared or should be a constant before it is used. In many instances assignment operator can be coupled with other operators to create a shorter statement version called Compound Statement. For instance, we can write sum + = 5 instead of sum = sum+5. +=, To add the left operand to the right operand and then assign it to the left operand variable. /=, To divide the left operand with the right operand and assign it to the left operand variable. -=, To subtract the left operand from the right operand and then assign it to the left operand variable. %=, To assign the left operand modulo to the right operand and then assign it to the left operand variable. *=, Multiply the left operand with the right operand and then assign it to the left operand variable. Unary Operators: Unary operators only require one operand. Used to increase, decrease, or deny/negate a value. ! : Logical not operator, It is used for inverting a boolean value. – :Unary minus, It is used for negating the values. + :Unary plus, It is used for giving positive values. Only used when deliberately converting a negative value to positive. ++ :Increment operator, It is used for incrementing the value by 1. There are two varieties of increment operator. Post-Increment : In thisvalue is first used for computing the result and then incremented. Pre-Increment : In thisvalue is incremented first and then result is computed. — : Decrement operator, It isused for decrementing the value by 1. There are two varieties of decrement operator. Post-decrement : In thisvalue is first used for computing the result and then decremented. Pre-Decrement : In thisvalue is decremented first and then result is computed. Relational Operators: These operators are used to verify relationships such as equality, larger than, smaller than. After comparison, they return boolean results and are widely used in looping statements as well as conditional statements if not otherwise. Few of the relational operators are below- !=, Not Equal to : It returns true if left hand side is not equal to right hand side. >, Greater than : It returns true if left hand side is greater than right hand side. >=, Greater than or equal to: It returns true if left hand side is greater than or equal to right hand side. ==, Equal to : It returns true if left hand side is equal to right hand side. <, less than : It returns true if left hand side is less than right hand side. <=, less than or equal to : It returns true if left hand side is less than or equal to right hand side. Bitwise Operators : These operators are used to perform manipulation of individual bits of a number. They can be used with any of the integer types. They are used when performing update and query operations of Binary indexed tree. &, Bitwise AND operator: returns bit by bit AND of input values. |, Bitwise OR operator: returns bit by bit OR of input values. ^, Bitwise XOR operator: returns bit by bit XOR of input values. ~, Bitwise Complement Operator: This is a unary operator which returns the one’s compliment representation of the input value, i.e. with all bits inversed. Logical Operators: In digital electronics, these operators are used to conduct "logical AND" and "logical OR," i.e. the feature comparable to AND gate and OR gate. One thing to remember is that if the first condition is incorrect, the second condition is not assessed, i.e. it has a short-circuiting impact. It is widely used to test multiple decision-making conditions.Logical operators are below-Logical OR (||): It returns true if at least one condition is true. Logical AND (&&): It returns true when both conditions are true. Ternary operator: Ternary operator is an if-else declaration shorthand variant. It has three operands, hence the ternary name. General format is-condition ? if true : if false The above declaration implies that if the condition is true, the statements will be executed after the '? ' otherwise execute after ': ' statements. Wrapper Class in Java A Wrapper class is a class whose item wraps or includes primitive data types. When we create an object in a wrapper class, it involves a field and it is possible to store primitive data types in this field. In other words, we can wrap a primitive value in an object of the wrapper class. Useswrapper classesin java mentioned below: Wrapper class is used to transform to objects from primitive data types. Objects are required if the arguments passed to a method are to be modified (because primitive types are passed by value and objects are passed by reference in java). All classes handle only objects in the java.util package and so wrapper classes also help in this scenario. In Collection framework in java, All classes such Set, List and Map store only objects and cannot work with primitive data types. In case of multithreading, an object is also needed to support synchronization. Primitive type in javaWrapper class in javabyteBytecharCharacterbooleanBooleanlongLongintIntegershortShortdoubleDoublefloatFloatAutoboxing in java: It means that automatic conversion to the object of their corresponding wrapper classes of primitive data types is known as autoboxing. Converting char to character, for example, long to Long, double to Double, etc. Unboxing in java: It's just the opposite technique of autoboxing. The conversion of a wrapper class object to its corresponding primitive type is known as unboxing. For example, converting Characters into char, Long to long, Double to double, etc. Decision Making Decision making structures have one or more conditions to be assessed or tested by the program, together with a declaration or statements to be performed if the situation is determined to be true, and optionally, other statements to be performed if the situation is found to be incorrect/false. Use if a given condition is true, to indicate a block of code to be performed. if (conditional statement) { // Code block to execute if the condition is true } Use If the same condition is incorrect, use another to indicate a block of code to be performed. if (conditional statement) { // Code block to execute if the condition is true} else { // Code block to execute if the condition is false } Use else if to specify a new condition to test, if the first condition is false. if (conditional statement 1) { // Code block to execute if the conditionstmt 1 is true } else if (conditional statement 2) { // block of code to execute if the conditionstmt1 is false and condition2 is true } else { // block of code to execute if the conditionstmt1 is false and conditionstmt2 is false } Use the switch to define a lot of alternative code blocks to execute. It is much like if-else. switch(expression) { case x: // code block to execute break; case y: // code block to execute  break; default: // code block to execute  } Point to remember in switch: The switch expression is evaluated once. The value of the expression is compared with the values of each case. If there is a match, the associated block of code is executed. When Java reaches a break keyword, it breaks out of the switch block. The default keyword specifies some code to run if there is no case match. Loops Loops can execute a block of code as long as a specified condition is reached. while loop: The while loop loops as long as a defined condition is true through a block of code: while (condition) { // code block to be executed  } do/while loop: A variant of the while loop is the do / while loop. This loop will perform the block of code once, then it will repeat the loop as long as the condition is true before checking if the situation is accurate. do { // code block to be executed  } while (condition);for loop: If you understand precisely how many times you want to loop through a code block, use the for loop instead of a loop for a while: for (statement 1; statement 2; statement 3) { // code block to be executed  } Statement 1: Before executing the code block, it is performed (one time). Statement 2: It describes the condition to execute the block of code. Statement 3: After executing the code block, it is performed (every time). Math Class in java The java.lang.Math class contains methods that are used for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions. Below is the declaration for java.lang.Math class – public final class Math    extends Object Some important methods of Math class below: MethodDescriptionabsReturns the absolute value of the argumentroundReturns the closed int or long (as per the argument)ceilReturns the smallest integer that is greater than or equal to the argumentfloorReturns the largest integer that is less than or equal to the argumentminReturns the smallest of the two argumentsmaxReturns the largest of the two arguments
logo

Java Tutorial

Basic Construct

  • Data Types in Java 

There are 2 types of data: 

  1. Primitive: The primitive data typesinclude boolean, char, byte, short, int, long, float and double. 
  2. Non-primitive: The non-primitive data types include classes, interfaces, and arrays. 
  • Numbers in java 

Number is the most primitive way of storing information in any computer language. 

You can store a number in a variable, but distinct formats are used to represent a number, and a separate quantity of storage is required for each format. 

The easiest format records a number that does not have a fractional part, i.e. it uses integers. 

You can choose to use one byte, two, four or eight bytes when you store an integer. The lower the amount of storage you use, obviously, the lower the range of numbers you can use. 

The Java integer data types are below: 

  1. byte: It can store 1 byte and ranges  -128 to 127 
  2. short: It can store 2 bytes and ranges -32,768 to 32,767 
  3. int: It can store  4 bytes and ranges -2,147,483,648 to 2,147,483,647 
  4. long: It can store 8 bytes and ranges -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 
  • Floating numbers in java 

You will now want to use numeric values with fractional parts, of course.. 

For reasons we don't have to go into, the format most used to store fractional numbers is called "floating point." A floating point variable may store a fractional number value, but you need to be conscious of the range that can be stored and the accuracy. 

Java has two kinds of floating points and it is hard to try to pin down the accuracy and range they can represent. Rather, the approximate amount of accuracy numbers they provide is easier to think about: 

  1. float: It can store 4 bytes and up to 7 decimal digits 
  2. double: It can store 8 bytes and up to16 decimal digits 
  • Characters 

In Java, the data type to store characters is char. 

Characters are represented using Unicode. Unicode is an international character set representing all the characters. 

In Java language, char ranges from 0 to 65,535. 

Characters are always written within ' '.For example: char first = 'f';  

  • Stringclass in java 

String is a series of characters because, for example, "Name" is a 4 character string. String is an immutable object in java which means it is constant and cannot be altered once it is formed. 

There are basically 2 ways to create a String object: 

1. By string literal: 

Double quotes are used to create Java String literal i.e. “Country” 

The JVM checks the "string pool" first every time you generate a string literal. If the string is already in the pool, a reference is returned to the pooled instance. If the string does not exist in the pool, it creates and places a new string instance in the pool. 

2. By new keyword: 

String s=new String("Country");//creates two objects and one reference variable 

In the above case, in normal (non-pool) heap memory, JVM will create a new string object, and the literal "Country" will be placed in the constant string pool. In a heap (non-pool), the variable s refers to the object. 

  • Unicode in java 

Unicode is a normal encoding 16-bit character and can represent nearly every character of the world's well-known languages. 

There were numerous standards for character encoding before Unicode: 

  • KOI-8: It was used for Russian. 
  • ASCII: It was used in the United States. 
  • ISO 8859-1: It was used for the Western European Language. 
  • GB18030 & BIG-5: It was used for Chinese. 

Some characters used a single byte, some two, to help multinational application codes. The same code may even represent a different character in one language and may represent other characters in another language 

The unicode scheme was created to solve the above shortcomings where each character is represented by 2 bytes. 

  • Strictfp keyword in java 

Strictfp is a Java programming language modifier that restricts the calculation of floating points to guarantee portability. The Java virtual machine (JVM) version 1.2 introduced the strictfp command into Java and is available for use on all currently updated Java VMs. 

Important points: 

  1. When a class or interface with a strtfp modifier is declared, then all methods declared in the class / interface and all nested types declared in the class are strictfp implicitly. 
  2. Only classes, interfaces and methods are used with the strictfp modifier. 
  3. Unable to use strictfp with abstract methods. However, abstract classes / interfaces can be used. 
  4. Since interface methods are implicitly abstract, within an interface strtfp cannot be used with any methods. 
  • Operators in java 

Java provides many types of operators which can be used according to the need. They are classified based on the functionality they provide. Some of the types are: 

Arithmetic Operators:They are used on primitive data types to conduct easy arithmetic operations. 

  1. % : Formodulo 
  2. + :  Foraddition 
  3. * :  Formultiplication 
  4. / :  Fordivision 
  5. – : Forsubtraction 

Assignment Operator : Assignment Operator i.e. ‘=’ is used to assign any variable to a value. It has a right-to-left associativity, i.e. the right-hand value of the operator is assigned to the left-hand variable and therefore the right-hand side value must be declared or should be a constant before it is used. 

In many instances assignment operator can be coupled with other operators to create a shorter statement version called Compound Statement. For instance, we can write sum + = 5 instead of sum = sum+5. 

  • +=, To add the left operand to the right operand and then assign it to the left operand variable. 
  • /=, To divide the left operand with the right operand and assign it to the left operand variable. 
  • -=, To subtract the left operand from the right operand and then assign it to the left operand variable. 
  • %=, To assign the left operand modulo to the right operand and then assign it to the left operand variable. 
  • *=, Multiply the left operand with the right operand and then assign it to the left operand variable. 

Unary Operators: Unary operators only require one operand. Used to increase, decrease, or deny/negate a value. 

  • ! : Logical not operator, It is used for inverting a boolean value. 
  • – :Unary minus, It is used for negating the values. 
  • + :Unary plus, It is used for giving positive values. Only used when deliberately converting a negative value to positive. 
  • ++ :Increment operator, It is used for incrementing the value by 1. There are two varieties of increment operator. 
    • Post-Increment : In thisvalue is first used for computing the result and then incremented. 
    • Pre-Increment : In thisvalue is incremented first and then result is computed. 
  • — : Decrement operator, It isused for decrementing the value by 1. There are two varieties of decrement operator. 
    • Post-decrement : In thisvalue is first used for computing the result and then decremented. 
    • Pre-Decrement : In thisvalue is decremented first and then result is computed. 

Relational Operators: These operators are used to verify relationships such as equality, larger than, smaller than. After comparison, they return boolean results and are widely used in looping statements as well as conditional statements if not otherwise. 

Few of the relational operators are below- 

  • !=, Not Equal to : It returns true if left hand side is not equal to right hand side. 
  • >, Greater than : It returns true if left hand side is greater than right hand side. 
  • >=, Greater than or equal to: It returns true if left hand side is greater than or equal to right hand side. 
  • ==, Equal to : It returns true if left hand side is equal to right hand side. 
  • <, less than : It returns true if left hand side is less than right hand side. 
  • <=, less than or equal to : It returns true if left hand side is less than or equal to right hand side. 

Bitwise Operators : These operators are used to perform manipulation of individual bits of a number. They can be used with any of the integer types. They are used when performing update and query operations of Binary indexed tree. 

  • &, Bitwise AND operator: returns bit by bit AND of input values. 
  • |, Bitwise OR operator: returns bit by bit OR of input values. 
  • ^, Bitwise XOR operator: returns bit by bit XOR of input values. 
  • ~, Bitwise Complement Operator: This is a unary operator which returns the one’s compliment representation of the input value, i.e. with all bits inversed. 

Logical Operators: In digital electronics, these operators are used to conduct "logical AND" and "logical OR," i.e. the feature comparable to AND gate and OR gate. One thing to remember is that if the first condition is incorrect, the second condition is not assessed, i.e. it has a short-circuiting impact. 

It is widely used to test multiple decision-making conditions.

Logical operators are below-

  • Logical OR (||):  It returns true if at least one condition is true. 
  • Logical AND (&&)It returns true when both conditions are true. 

Ternary operator: Ternary operator is an if-else declaration shorthand variant. It has three operands, hence the ternary name. 

General format is-condition ? if true : if false 

The above declaration implies that if the condition is true, the statements will be executed after the '? ' otherwise execute after ': ' statements. 

  • Wrapper Class in Java 

A Wrapper class is a class whose item wraps or includes primitive data types. When we create an object in a wrapper class, it involves a field and it is possible to store primitive data types in this field. In other words, we can wrap a primitive value in an object of the wrapper class. 

Useswrapper classesin java mentioned below: 

  • Wrapper class is used to transform to objects from primitive data types. Objects are required if the arguments passed to a method are to be modified (because primitive types are passed by value and objects are passed by reference in java). 
  • All classes handle only objects in the java.util package and so wrapper classes also help in this scenario. 
  • In Collection framework in java, All classes such Set, List and Map store only objects and cannot work with primitive data types. 
  • In case of multithreading, an object is also needed to support synchronization. 
Primitive type in javaWrapper class in java
byteByte
charCharacter
booleanBoolean
longLong
intInteger
shortShort
doubleDouble
floatFloat

Autoboxing in javaIt means that automatic conversion to the object of their corresponding wrapper classes of primitive data types is known as autoboxing. Converting char to character, for example, long to Long, double to Double, etc. 

Unboxing in javaIt's just the opposite technique of autoboxing. The conversion of a wrapper class object to its corresponding primitive type is known as unboxing. For example, converting Characters into char, Long to long, Double to double, etc. 

  • Decision Making 

Decision making structures have one or more conditions to be assessed or tested by the program, together with a declaration or statements to be performed if the situation is determined to be true, and optionally, other statements to be performed if the situation is found to be incorrect/false. 

  • Use if a given condition is true, to indicate a block of code to be performed. 
if (conditional statement) {
// Code block to execute if the condition is true 
  • Use If the same condition is incorrect, use another to indicate a block of code to be performed. 
if (conditional statement) {
// Code block to execute if the condition is true} else { 
// Code block to execute if the condition is false 
  • Use else if to specify a new condition to test, if the first condition is false. 
if (conditional statement 1) {
// Code block to execute if the conditionstmt 1 is true 
} else if (conditional statement 2) {
// block of code to execute if the conditionstmt1 is false and condition2 is true 
} else {
// block of code to execute if the conditionstmt1 is false and conditionstmt2 is false 
} 
  • Use the switch to define a lot of alternative code blocks to execute. It is much like if-else. 
switch(expression) {
case x:
// code block to execute 
break;
case y:
// code block to execute 
break;
default:
// code block to execute 
} 

Point to remember in switch: 

  1. The switch expression is evaluated once. 
  2. The value of the expression is compared with the values of each case. 
  3. If there is a match, the associated block of code is executed. 
  4. When Java reaches a break keyword, it breaks out of the switch block. 
  5. The default keyword specifies some code to run if there is no case match. 
  • Loops 

Loops can execute a block of code as long as a specified condition is reached. 

  • while loop: The while loop loops as long as a defined condition is true through a block of code: 
while (condition) {
// code block to be executed 
} 
  • do/while loop: A variant of the while loop is the do / while loop. This loop will perform the block of code once, then it will repeat the loop as long as the condition is true before checking if the situation is accurate. 
do {
// code block to be executed 
}
while (condition);
  • for loop: If you understand precisely how many times you want to loop through a code block, use the for loop instead of a loop for a while: 
for (statement 1; statement 2; statement 3) {
// code block to be executed 
} 

Statement 1Before executing the code block, it is performed (one time). 

Statement 2It describes the condition to execute the block of code. 

Statement 3After executing the code block, it is performed (every time). 

  • Math Class in java 

The java.lang.Math class contains methods that are used for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions. 

Below is the declaration for java.lang.Math class – 

public final class Math 

   extends Object 

Some important methods of Math class below: 

MethodDescription
absReturns the absolute value of the argument
roundReturns the closed int or long (as per the argument)
ceilReturns the smallest integer that is greater than or equal to the argument
floorReturns the largest integer that is less than or equal to the argument
minReturns the smallest of the two arguments
maxReturns the largest of the two arguments

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

protective orders in virginia

Keep sharing blogs like this one; they are quite good. You have given everyone in this blog access to a wealth of information.

Johnfrance

Thank you for your valuable information.

sam

Thank you for this wonderful article!

Belay Abera

This article was really helpful to me, thank you!

dfsdf

super article!