TIL : Java Non-Primitive Data Type

Adam Sung Min Park·2022년 9월 25일
0

Java

목록 보기
1/1
post-thumbnail

Yesterday was about primitive data type. Now its time to learn about non-primitive types.

Non Primitive Data Types

user defined data types unlike primitive types and they can be easily created or modified by the users. They are used to store multiple values of either the same data type or different data types. (Ex: array)

Whenever non-primitive data type is defined, it gives reference to the memory location where the data is stored.
=> whenever non primitive data type is passed on to a function or a var, we are simply passing the address where the data is stored . (right part of the diagram is today's topic)

  • Class
    • used to create object
    • contains a set of properties and methods.
    • consist of Access Modifier, Class Name, Superclass(only when it needs to extend /inherit from its parent class), Interfaces (with the word "implements") and body.

Example

class Demo{     //Class name - Demo having first inital letter in capital
    int res = 100;    //Class member variable
    Demo(){          //Class default constructor
        System.out.println(res);
    }
    public void add(int a,int b){   //Class member function
        int c = a + b;
        System.out.println("Addition of numbers: " + c);
    }

    public void sub(int a,int b){  //Class member function
        int c = a - b;
        System.out.println("Subtraction of numbers: " + c);
    }
}

public class Main{
    public static void main(String[] args) {
        Demo obj = new Demo();
        obj.add(10,20);
        obj.sub(50,25);
    }
}

Output

100
Addition of numbers: 30
Subtraction of numbers: 25
  • Object
    • var of class by which member functions are accessible.
    • State(shows the props of an object and represented by the attributes of the obj.)
    • Identity(unique name)
    • Behavior(shows the response of an obj to other obj and is represented by the methods of the obj)
  • String
    • sequence of characters.
    • there is a method called substring
      • Example
		public class SubStringExample {  
    public static void main(String[] args) {  
            
        String str = "Hello!";  
   
        String subStr = str.substring(0,3);  
  
        System.out.println(subStr);  
    }  
}  

the above code will output : "Hell"

  • Array
    • Data type which can store multiple same type of variables. (integers only or strings only etc.)
    • Can be accessed using the index.
    • Variables can be either primitive / non- primitive.
  • Interface
    • Similar to class but the difference is that its method are abstract by default. (do not have a body)
    • Consist of only the final variables and method declarations.
    • AKA Fully Abstract Class
      • Example
/Interface declaration: by first user  
interface Drawable{  
void draw();  
}  
//Implementation: by second user  
class Rectangle implements Drawable{  
public void draw(){System.out.println("drawing rectangle");}  
}  
class Circle implements Drawable{  
public void draw(){System.out.println("drawing circle");}  
}  
//Using interface: by third user  
class TestInterface1{  
public static void main(String args[]){  
Drawable d=new Circle();//In real scenario, object is provided by method e.g. getDrawable()  
d.draw();  
}}  

Output
drawing circle

0개의 댓글