STUDENTS 3K

  • Home
  • Students+
    • Engineering Students
    • Education Loan
    • Study Abroad Guide
    • Projects Download
  • Freshers
    • Aptitude Q & A
    • Placement Papers
    • Verbal Ability
    • Interview Questions
    • IT Company Details
    • Job Updates
  • Study Resources
    • Career Guidance
    • LAB Programs
      • C Programs
      • CPP Programs [C++]
      • Java Programs
    • Question Papers
    • Learn English
    • Notice Board
  • More –>>
    • Love calculator
You are here: Home / Interview / Difference Between Class and Primitive Types

Difference Between Class and Primitive Types

All the variables declared in Java are classified into certain type. The classification of variables will help us in treating the variables according to its type. Variables in Java are classified as characters, several types of integers, floating points and types to declare a statement true or false. All the above mentioned variable types are called as primitive types.

  • Related: SQL & .NET Interview questions and Answers

Class Type Variables in Java

Class type variables are used to solve the object oriented problems. Separate types of variables are used to solve the object oriented programs because they are more complex than simple integer and boolean functions. The variable which names a certain object in a program is classified into class type variables.

  • Read: More Interview Questions

Difference between Class and Primitive types:

  • Every variable declared in a program is stored in the memory of the computer. The Storing method of class type variables is different from primitive type. The class type variable stores only the memory address of the variable but not its value. If a variable is declared in primitive it stores the value of the variable in the particular memory location.
  • The primitive data type always has a value and never be declared as null. If no value is assigned for a primitive data type, a default value of zero will be initialized.
  • When a value is assigned to a primitive data type, it will be copied but only the handle is copied in class data type. The class data type will share the variables so change in one variable will affect the other.
  • When the equality (==) operator is used in primitive data types only its primitive values are compared. If equality operator is used in class data type, the address which stored the data is compared. Ex: imagine that we have two int variables namely a and b whose value is 10. If a is compared with b using equality operator, the result will declared that the two integers are equal. If two strings namely “USA” and “USA” are compared using “equality” operator the result will state that the strings are not same.
  • When primitive values are passed into a method, its values are copied. When class values are passed into method only the handle of class types are copied.
  • Primitive type returns its value while only the handle is returned in the case of class type.
  • Primitive types are stored in stack while class type are stored as heap which is managed by garbage collector.
  • The memory space consumed by primitive data types is very less when compared to the memory type of class type. Class type consumes more memory because it maintains metadata objects.
  • Primitive data types can be copied but class data types can be shared.
  • All primitive data types have an default value but uninitialized class data types will throw NullPointerException.

Converting Primitive Types into Class Types – Vice Versa

Casting is a process of converting a data type into another. All primitive and class types can be converted into another using the casting process. In Java, the java.lang package is comprised of classes which correspond to all primitive data types. The names of classes present in java.lang package are almost similar to the names of data types. Utilizing the classes in java.lang package, it is possible to create a class type which can create two objects with same value.

Converting a string into a primitive type is a common translation used in Java programming. The primitive data type created by converting a string can be used anywhere in the program without altering its value. Consider the following code as an example:

String Bollywood=“1001”;

int b= Integer.parseInt(Bollywood);

In the example code given above, the class Integer is used to convert the string “Bollywood” into “1001”. Similarly classes like Boolean, Float, Double, Void, Short and Long are also used.

Database Roles in SQL

Autoboxing and unboxing

Autoboxing and unboxing are the process used to convert the one variable type into another automatically.

Autoboxing is used to convert the primitive type into class type while unboxing is used to convert class type into primitive type. If a statement is written in primitive or class type, it is converted vice versa according to the requirement of the compiler. Conversion using Autoboxing and unboxing works only if the type has a value. If objects which are called using constructors are converted, it will result in a compiler error.

Filed Under: Interview Tagged With: class, Interview, SQL





Random Materials :

  • Data Mining and Data Warehousing
  • Difference Between Class and Primitive Types
  • Database Roles in SQL
  • CASE Expression in SQL
  • Concurrent Update Problem

Follow Us on Facebook

Advertisement

Company Profile

  • About Students3k
  • Contact Us
  • Earn Money Online
  • Website Archive Page
  • Privacy Policy
  • Disclaimer

Categories

  • Important Programs
  • Study Abroad
  • IT Companies
  • Career Guidance for Students
  • Teachers
  • Verbal Analogies

We Are Social

  • Facebook Fans
  • Twitter Follow
  • Google Plus
  • Pinterest Page

© Copyright 2012 - 2016 Students3k.com · All Rights Reserved · Designed By Benefits

Show