There are 2 types of data:
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:
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:
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';
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 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:
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 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:
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.
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.
Unary Operators: Unary operators only require one operand. Used to increase, decrease, or deny/negate a value.
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-
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.
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-
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.
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:
Primitive type in java | Wrapper class in java |
---|---|
byte | Byte |
char | Character |
boolean | Boolean |
long | Long |
int | Integer |
short | Short |
double | Double |
float | Float |
Autoboxing 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 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.
if (conditional statement) { // Code block to execute if the condition is true }
if (conditional statement) { // Code block to execute if the condition is true} else { // Code block to execute if the 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 }
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:
Loops can execute a block of code as long as a specified condition is reached.
while (condition) { // code block to be executed }
do { // code block to be executed } while (condition);
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).
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:
Method | Description |
---|---|
abs | Returns the absolute value of the argument |
round | Returns the closed int or long (as per the argument) |
ceil | Returns the smallest integer that is greater than or equal to the argument |
floor | Returns the largest integer that is less than or equal to the argument |
min | Returns the smallest of the two arguments |
max | Returns the largest of the two arguments |
Keep sharing blogs like this one; they are quite good. You have given everyone in this blog access to a wealth of information.
Thank you for your valuable information.
Thank you for this wonderful article!
This article was really helpful to me, thank you!
super article!
Leave a Reply
Your email address will not be published. Required fields are marked *