There are various data types in Java or in any computing language. for example:- char, boolean, float,...........
For a quick overview, we will be learning:
Ways to print
Variables
Primitive data types ( 8 types )
Nonprimitive data types ( 5 types )
How to take an input
Ways to print:
1) print
public class Main {
public static void main(String[] args){
System.out.print("hello");
System.out.print("hello");
}
}
This code will give you an output as
hellohello
2)Println
public class Main {
public static void main(String[] args){
System.out.println("hello");
System.out.println("hello");
}
}
This code will output as
hello
hello
You can also print this same output by using "\n" where you want a new line
public class Main {
public static void main(String[] args){
System.out.print("hello\n");
System.out.print("hello");
}
}
Variables
In mathematical terms, the variable is a quantity that may assume any one of a set of values. It can vary.
To call it we just need to specify it as:
int a = 35;
Here we need to tell our computer if it is a number, a string(ex: name), etc.
a is the name of the variable. Think of it as 35 numbers stored in a box. The name of the box is a.
Let's say we made another box such as
int b = 10;
a and b are stored in computer memory with 35 and 10.
If we want to sum them we just write
int sum = a+b;
Instead of writing the whole System.out.print just write syso and select it. It will get automatically print.
Primitive data types
Primitive data types are already defined in Java. It always has value.
There are 8 types of Primitive Data types:
Byte -- Stores whole numbers from -128 to 127
Short -- Stores whole numbers from -32,768 to 32,767
Char -- Stores whole numbers from -2,147,483,648 to 2,147,483,647
Boolean -- Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Int -- Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
Long -- Stores fractional numbers. Sufficient for storing 15 decimal digits
Float -- Stores true or false values
Double -- Stores a single character/letter or ASCII values
Non-Primitive Data Types
These data types are created by us(except strings). can be null. All have the same sizes.
String - sequence of characters (for eg: "hello world", "hi")
Array - the container holding a fixed number of values of single type .for eg: int[] myArray = {10, 20, 30, 40}
Class - template used to create objects and to define object data types and methods. for eg:
class addition{
int a = 23;
int b = 13;
int sum = a+b;
System.out.println(sum);
}
object - they refer to any particular object. Examples include arrays, strings, classes, interfaces etc.
interface - You can declare a class as abstract when it contains zero or more abstract methods or When an interface is implemented to a class where not all methods are not implemented.
Remember:1 byte = 8bits[memory]
How to take an input
We have a file called java. it consists of various libraries. To import it we write:
import java. util.*;
Here Scanner is given name as sc. To take input just write sc.next();
import java.util.*;
public class Main{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(
System.in
);
String name =
sc.next
();
}
}
Here your string input is saved in a container called a name. You can take inputs using various data types as
Float percentage =
sc.next
();
Int age = sc.nextInt();
Char password = sc.nextChar();