What are Java's primitive types? [duplicate]
Asked Answered
I

14

42

What are primitive type in Java?

What is the difference between a primitive type and a reference type?

How many primitive types does Java have, and what are they?

Impractical answered 3/5, 2012 at 11:38 Comment(4)
In addition to the answers below, it might help to also know the difference between variables of primitive data type and scalar variables as sometimes the distinction is not apparent: https://mcmap.net/q/134628/-scalar-vs-primitive-data-type-are-they-the-same-thing/346232Nutrition
Data type which holds values only are called primitive data types(byte,short,int etc.). And the other data types which contains references are called as reference data type (Integer,Boolean,String etc...).Sayyid
javaseeeedu.blogspot.com/2015/08/data-types-in-java.htmlAzole
see this, smugjava.blogspot.in/2017/11/data-types-in-java.htmlCheryllches
F
53

In Java, every variable has a type declared in the source code. There are two kinds of types: reference types and primitive types. Reference types are references to objects. Primitive types directly contain values. There are 8 primitive types:

  • byte
  • short
  • int
  • long
  • char
  • float
  • double
  • boolean
Faus answered 3/5, 2012 at 11:41 Comment(3)
also, the reference to the Object are passed by value.Socialite
@sufiyan: correct, but what does that have to do with this question?Faus
nothing in particular, i just want the future readers not to get confused with this statement "references to objects.", since a lot of beginners starts to assume that Java is pass-by-reference based on the statement "Reference types are references to objects".Socialite
T
8

What do people mean by "Types"?

In the real world there are different "types" of things. e.g. There are many different "types" of vehicles: "sports cars" for driving fast, "utes" for carrying tools, "trucks" for transporting lots of goods, and limousines for traveling in luxury.

In the same way, in Java, you can have different "types" of data which serve different purposes: e.g. numbers (are used to add/subtract etc), "strings" are used to communicate words and letters. You cannot use letters to add - that just does not make sense, nor could you use numbers to write a sentence.

Primitives vs reference types - what does it mean? What's the difference?

Now there are some "types" of data which are basic. These are already created by the boffins at Redmond/Sun. These are called "primitive" java types, and they store the values within themselves. What does that mean? It's best explained by example:

Example of a primitive type

If I gave you a $50 note, then the note in and of itself is worth $50. The value is stored in the note itself.

Primitives Juxtaposed with Reference Types

Imagine that instead of giving you $50 I gave you an piece of paper which has on it an address to a safe deposit box. The piece of paper i gave you is not worth $50 in and of itself, but it points to an address where you can get your $50. This piece of paper is basically a "reference" type, because it doesn't store any values within and in and of itself, it merely points to certain addresses.

But I can give you an address to anything: planes, castles, rainforrests: anything!

Summary

You can't just hand someone a plane or a Shinkansen train from your back pocket: you just hand them an address to it. Juxtapose this with having $50, or any type of currency: the actual substance is in your back pocket.

That in an nutshell is the difference between primitive and reference types.

(Corny analogy used to help you understand and remember.)

Tricky answered 27/10, 2016 at 21:47 Comment(0)
G
6

From the Java Language Specification, Chapter 4. Types, Values, and Variables:

The Java programming language is a statically typed language, which means that every variable and every expression has a type that is known at compile time.

The Java programming language is also a strongly typed language, because types limit the values that a variable [...] can hold or that an expression can produce, limit the operations supported on those values, and determine the meaning of the operations. Strong static typing helps detect errors at compile time.

The types of the Java programming language are divided into two categories: primitive types and reference types. The primitive types [...] are the boolean type and the numeric types. The numeric types are the integral types byte, short, int, long, and char, and the floating-point types float and double. The reference types [...] are class types, interface types, and array types. There is also a special null type. An object [...] is a dynamically created instance of a class type or a dynamically created array. The values of a reference type are references to objects. All objects, including arrays, support the methods of class Object [...].

Glandulous answered 3/5, 2012 at 12:3 Comment(0)
F
4

Primitive types in Java are none-class types. They only store values.

double d = 3.0;
d.intValue();  //compiler error!
Double d2 = new Double(3.0);
d2.intValue();  //works!
Foursome answered 3/5, 2012 at 11:40 Comment(0)
I
3

There are reference types, primitives types and void

For each primitive types (and void) there is a wrapper type which defines a constant called TYPE which have the class of the primitive type.

A compiler way to get a class for a primitive type is to use the .class notation. e.g.

Class<Integer> intClass = int.class; // == Integer.TYPE
Class<Void> voidClass = void.class; // == VOID.TYPE
Ideo answered 3/5, 2012 at 11:44 Comment(0)
C
1

They are non class types which only hold a value. While passing a primitive variable, you are passing the value itself instead of the reference of the value.

Caoutchouc answered 3/5, 2012 at 11:44 Comment(0)
P
1

java support basic data type int, double, float etc. total eight. these are the primitive type data or basic type of your data.

boolean,char,byte,short,int,long,double,float

Philine answered 3/5, 2012 at 11:50 Comment(0)
K
1

Primitive types are not classes, but can be used to store values like numbers and characters.

  • byte
  • short
  • int
  • long
  • char
  • float
  • double
  • boolean
Knockout answered 3/5, 2012 at 12:6 Comment(0)
P
1

Any data type built-in to a programming language is called primitive data type. Words primitive or built-in or basic data types are used interchangeably by authors. Primitive data types in Java are provided by the Java programming language as a basic building block and for that they are called primitive types to Java. Java also allows programmers to define their own types (user defined types). For primitive types Java has built-in support. Primitive types are predefined by the Java language and are named by a reserved keywords. Java supports 8 built-in data types and their basic behavior and supported operations cannot be modified by programmers.

Pict answered 16/12, 2013 at 8:51 Comment(0)
L
1

Actual meaning of primitive is whether datatypes are classes or not.Java is not a pure object oriented language because in java datatypes are not primitives means datatypes are not classes.

Eg:- int a; a=10;

/*Integer a=new Integer();
     a=10;
*/not possible

while in c# datatypes are classes and thats why it is pure object oriented language

Landowner answered 19/9, 2015 at 16:43 Comment(0)
C
1

There are two types of data type, Primitive and Object reference. Primitive data types are not object, they just only store the actual values, whereas object reference variable store the address of the object they refer to.

Chargeable answered 7/4, 2016 at 23:11 Comment(0)
I
0

The eight primitive data types that are supported by Java Programming Language:

  • byte - It is 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127.

  • short - It is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767.

For detailed explanation Click Here

Inheritrix answered 18/8, 2013 at 11:30 Comment(0)
J
0

There are two divisions in data types which are primitives and reference . The primitives will represent the following

  • Numbers
  • Boolean
  • Float
  • Double
  • Char

Under the numbers we have

  1. Byte: -128 to 127
  2. Short: -32,768 to 32,767
  3. Int: -2^31 to 2^31
  4. Long: -2^63 to 2^63 - 1

These will represent numbers and have different memory allocations to save memory we can use it efficiently. Among int and long we have signed & unsigned. Unsigned starts with min=0.

Jehol answered 29/3, 2017 at 9:0 Comment(1)
Can be more descriptivePhotogravure
I
0

In Java has two kinds of type,

  • Primitive types
  • Reference types

Primitive types

Primitive types directly contain values.

int i = 24;

There are 8 primitive types,

  1. byte
  2. short
  3. int
  4. long
  5. char
  6. float
  7. double
  8. boolean

Reference types

Reference types are references to objects. When you create new Test object inside the Test.java class there is reference type

Test t = new Test();

Specially String is a reference type. It's not a primitive type. There are two way create String object.

String s = "abc";

String y = new String("abc");
Iliad answered 31/10, 2017 at 9:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.