Primitive Data Type Vs. Object Data Type in JavaJava Primitive Data TypesPrimitive data types in Java are the building blocks of data manipulation. They are the most basic data types available in the Java language. Java is a statically-typed programming language, which means that all variables must be declared before they are used. Primitive data types allow storing only one value at a particular location. They are predefined in the Java language and occupy a fixed amount of memory. The eight primitive data types supported by the Java programming language, which can be categorized into four groups: integers, floating-point numbers, characters, and boolean.
PrimitiveType.java Output: smallNumber: 10 smallInteger: 1000 integer: 1000000 bigInteger: 10000000000 decimalNumber: 3.14 bigDecimal: 123.456789 character: A isJavaFun: true isProgrammingHard: false
Default Values of Primitive Data TypeIt is not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.
Using primitive data types is efficient in terms of memory usage and performance because they are not objects and are stored directly in memory. Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If we cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error. Object Data Type in JavaThese are also referred to as Non-primitive or Reference Data Type. They are so-called because they refer to any particular object. Unlike the primitive data types, the non-primitive ones are created by the users in Java. Object data types can be used to store more complex data than primitive data types. For example, a class can store data for a person, such as their name, age, and address. A string can store text, such as a sentence or a paragraph. An array can store a collection of data, such as a list of numbers or a list of strings. When the reference variables will be stored, the variable will be stored in the stack and the original object will be stored in the heap. In Object data type although two copies will be created they both will point to the same variable in the heap, hence changes made to any variable will reflect the change in both the variables. Example of Object Data Types:Classes, Interfaces, Strings, Arrays, Collections. Default Values of Object Data TypeIn Java, for object data types (reference types), the default value is always null. This means that when you declare a variable of an object data type (e.g., a class or interface), it does not have a meaningful value until you assign an actual object instance to it. Difference Between Primitive Data Types and Object Data Types
Next TopicsetBounds() Method in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India