+ 1
What is Value types and explain in simple example?
I don't understand value types in tutorial.
5 Answers
+ 1
There are [primitive data type]s (also called value types), [interface]s and [class]es.
You can define both interfaces and classes yourself, they are referred to using references.
Value types on the other hand are given and you cannot create new value types. Those are listed on the following page:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
They can have only predefined values.
In a nutshell, value types are the simplest types in Java. There are:
+ Logical: boolean (1 (8) bit)
+ Integers: byte, short, int, long (8,16,32,64 bits)
+ Fractions: float, double (32,64 bits)
+ Text: char (16 bits)
All primitive data types are immutable, which means, they are values, which cannot be modified. Like the number 5 cannot be modified itself. You can replace the content of the variable storing 5 to store 6, but that is another value.
Example variables:
| boolean b1 = true;
| byte b2 = (byte)10;
| short s = (short)10;
| int i = 10;
| long i = 10L;
| float f = 0.5f;
| double d = 0.5;
| char c = 'x';
Maybe, most easily I would explain as.... there are VALUES and OBJECTS, which are present in a program. Values are of value types, like integer, float, long... they have nothing special about them, they are just simple values. And there are objects, which are of a given class. Not as primitive data types, classes can have fields and methods. Values are referred to using their value, like '5' or 'true', meanwhile creating objects require the 'new' keyword:
| int x = 10;
| Object o = new Object();
+ 1
The 'String' type is a very special one. It is not a primitive data types, but it is handled sometimes as such internally or syntactically. Don't be deceived however, it is immutable, but a class type, not a primitive data type.
The following line is tricky:
| String s = "Hello World";
It is much more like this behind the scenes:
| String s = new String("Hello World");
But more specially handled by Java.
+ 1
The main difference comes from the fact, that values are copied, while objects are passed pointing to the same object. Let's see some examples.
int a = 5; //var a now has the value: 5
int b = a; //var b now has the value: 5
b = b + 1; //var b is set to value: 6
In the end, a is 5 and b is 6. After the second line, variables 'a' and 'b' both contain the value 5, but there is no connection between them. You cannot modify the 'b' variable to change value inside 'a'.
But if you use objects, like lists:
List<String> a = new List<String>();
List<String> b = a;
b.add("Hello World");
The first line creates a new List object to store String's, then stores the list in the 'a' variable. The second line stores the list from variable 'a' in variable 'b'. Now both 'a' and 'b' do store the same list object. If I add the string "Hello World" to the list within 'b', then the list within 'a' will also contain it; because it is the same list stored in the two variable, the same thing is "called" 'a' and 'b'.
The same goes to method calls:
int x = 30;
someMethod(x);
//which is like: static void someMethod(int asd) { ... }
//'x' is for sure equals to 30
Whatever happens inside the someMethod(int) method, the x variable will not change whatsoever. If I change the 'asd' variable to another number, for example, increase it by one inside the method, 'asd' will change to 31, but 'x' will remain 30. The value from inside 'x' (30) is copied and used inside the method, but 'x' cannot change due to the call. But what if the method gets a list object?
List<String> strings = new List<String>();
someMethod2(strings);
//which is like: static void someMethod2(List<String> asd2) { ... }
//list stored in 'strings' might be changed
In this case, if someMethod2 adds an element to the list received, then - because 'strings' and 'asd2' refers to the same list - the list inside 'string' will have the elements after the call.
(Note, that the list objects remains the same - the content of the list is what changed.)
+ 1
That is not a big deal whenever a type is primitive or class type, until you use them as generic types, like you want to create a list of something.
You can create: List<String>
But you cannot: List<int>
You must use: List<Integer>
Every primitive value type has its "wrapper" object:
| boolean -> Boolean
| byte -> Byte
| short -> Short
| int -> Integer
| long -> Long
| float -> Float
| double -> Double
| char -> Character
So, if you want a list of floats, then you use:
List<Float> floats = new List<Float>();
Array are not restricted through, for example you can use both:
+ int[] intArray1 = new int[9];
+ Integer[] intArray2 = new Integer[9];
There is some difference between the two, but that is another topic. If you are interested search/ask about where to use primitive values and where to use their wrapper objects. But for simplicity, use int[] instead of Integer[] and so where you can, it is rarely if anytime a good thing to use Integer[].
+ 1
Thank u