Data Types in Java || Java + DSA course || Blog 2

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 )
  • Non primitive data types ( 5 types )
  • How to take a input 

If you didn't read our first blog , please checkout https://javaanddsa.blogspot.com/2022/05/introduction-to-java-java-dsa-course.html#more

 

 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 a mathematical term,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 a name of the variable. Think of it as 35 number is stored in a box. The name of the box is a.

Lets say we made another box such as ,

int b = 10;

a and b are stored in computers 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 a 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 same sizes.

  • String - sequence of characters (for eg: "hello world", "hi")
  • Array - container holding 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 objects. 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 as java. it consists of various libraries. To import it we write:

import java.util.*;

Here Scanner is given name as sc. To take an 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 as name. You can take inputs using various data types as

Float percentage = sc.next();
Int age = sc.nextInt();
Char password = sc.nextChar();

 ___________________________________________________________________________________

did you like our Blog 2?

Tell us in the comment section

if you didn't read our first blog , please checkout https://javaanddsa.blogspot.com/2022/05/introduction-to-java-java-dsa-course.html#more

Comments

Popular posts from this blog

LinkedList Problems for Beginners DSA || Java and DSA course || Blog 17

Introduction to Java || Java + DSA course || Blog 1

LinkedList in DSA || Java and DSA course || Blog 16