Tutorial Playlist
191 Lessons1. Introduction to Java
2. What is Java?
3. History of Java
4. Java Tutorial for Beginners
5. How Do Java Programs Work?
6. JDK in Java
7. C++ Vs Java
8. Java vs. Python
9. Java vs. JavaScript
10. From Java Source Code to Executable
11. How to Install Java in Linux
12. How to Install Java in Windows 10
13. Java Hello World Program
14. Structure of Java Program and Java Syntax
15. Operators in Java
16. Java If-else
17. Switch Case In Java
18. Loops in Java
19. Infinite loop in Java
20. For Loop in Java
21. For Each Loop in Java
22. Constructor in Java
23. Constructor Overloading in Java
24. Copy Constructor in Java
25. Default Constructor in Java
26. Parameterized Constructors in Java
27. Constructor Chaining In Java
28. Finalize Method in Java
29. Static Method in Java
30. Equals Method in Java
31. Abstract Method in Java
32. toString() Method in Java
33. Difference between equals method in Java
34. Inheritance in Java
35. Multiple Inheritance in Java
36. Hierarchical Inheritance in Java
37. Java Classes and Objects
38. Scanner Class in java
39. All classes in java are inherited from which class
40. What is Nested Class in Java
41. POJO Class in Java
42. Anonymous Class in Java
43. Final Class in Java
44. Object Class in Java
45. Packages in Java
46. Access Modifiers in Java
47. Static Keyword In Java
48. Final Keyword in Java
49. Checked and Unchecked Exceptions in Java
50. User Defined Exception in Java
51. Error vs. Exception in Java
52. Java Collection
53. Collections in Java
54. Garbage Collection in Java
55. Generics In Java
56. Java Interfaces
57. Functional Interface in Java
58. Marker Interface in Java
59. Streams in Java
60. Byte stream in java
61. File Handling in Java
62. Thread in Java
63. Thread Lifecycle In Java
64. Daemon Thread in Java
65. Thread Priority in Java
66. Deadlock in Java
67. String Pool in Java
68. Java Database Connectivity(JDBC)
69. Design Patterns in Java
70. Functional Programming in Java
71. OOP vs Functional vs Procedural
72. Heap Memory and Stack Memory in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
76. Hibernate Framework
77. JUnit Testing
78. How to Install Eclipse IDE for Java?
79. Command line arguments in Java
80. Jar file in Java
81. Java Clean Code
82. OOPs Concepts in Java
83. Java OOPs Concepts
84. Overloading vs Overriding in Java
85. Java 8 features
86. String in Java
87. String to int in Java
88. Why String Is Immutable in Java?
89. Primitive Data Types in Java
90. Non-Primitive Data Types in Java
Now Reading
91. This and Super Keyword in Java
92. HashMap in Java
93. Comparable And Comparator in Java
94. Type Casting in Java
95. Arrays Sort in Java with Examples
96. Variable Hiding and Variable Shadowing in Java
97. Enum in Java
98. Substring in Java
99. Pattern Programs in Java
100. Hashcode in Java
101. What is ByteCode in Java?
102. How To Take Input From User in Java
103. GCD of Two Numbers in Java
104. Linked List in Java
105. Arithmetic Operators in Java
106. Conditional Operators in Java
107. Stack and Queue in Java
108. Array Length in Java
109. Number Pattern Program in Java
110. Split in java
111. Map In Java
112. Difference Between Throw and Throws in Java
113. Difference Between Data Hiding and Abstraction
114. HashSet in Java
115. String Length in Java
116. Factorial Using Recursion in Java
117. DateFormat in Java
118. StringBuilder Class in java
119. Instance variables in Java
120. Java List Size
121. Java APIs
122. Reverse an Array in Java
123. StringBuffer and StringBuilder Difference in Java
124. Java Program to Add Two Numbers
125. String to Array in Java
126. Regular Expressions in Java
127. Identifiers in Java
128. Data Structures in Java
129. Set in Java
130. Pass By Value and Call By Reference in Java
131. Try Catch in Java
132. Bubble Sort in Java
133. Caesar Cipher Program in Java
134. Queue in Java
135. Object Creation in Java
136. Multidimensional Array in Java
137. How to Read a File in Java
138. String Comparison in Java
139. Volatile Keyword in Java
140. Control Statements in Java
141. Jagged Array in Java
142. Two-Dimensional Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
147. Matrix Multiplication in Java
148. Static Variable in Java
149. Event Handling in Java
150. parseInt in Java
151. Java ArrayList forEach
152. Abstraction in Java
153. String Input in Java
154. Logical Operators in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
159. Stringtokenizer in java
160. Implementing and Manipulating Abs in Java
161. Char array to string in java
162. Convert Double To String In Java
163. Deque in Java
164. Converting a List to an Array in Java
165. The Max function in java
166. Removing whitespace from string in java
167. String arrays in Java
168. Strings in Java Vs Strings in Cpp
169. Sum of digits of a number in Java
170. Art of Graphical User Interfaces
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
175. Difference Between Java and Python
176. Square Root in Java
177. Reverse A String in Java
178. Even Odd Program in Java
179. Fibonacci Series in Java
180. Prime Number Program in Java
181. Java Program to Print Prime Numbers in a Given Range
182. Java Leap Year Program
183. Swapping of Two Numbers in Java
184. LCM of Two Numbers in Java
185. Math.sqrt() Function in Java
186. Area of Triangle in Java
187. Sort a String In Java
188. Factorial Program in Java
189. Javafx
190. Lambda expression in java
191. Setup Java Home and IDE on macOS
Non-primitive data types in Java are data types that are not integrated and allow more complicated and configurable structures and behaviors. They include classes, interfaces, arrays, and enums, which let developers build and operate objects with multiple values and references.
Non-primitive data types are user-defined data types beyond the built-in or fundamental ones. These data types, including classes, interfaces, and arrays, enable the creation of customized structures and behaviors.
Non-primitive data types in Java provide versatility by allowing developers to build their data structures and operate complicated objects.
Data types in Java categorize and define the data types variables can store. Primitive data types in Java represent simple values such as boolean, byte, int, long, and char.
And each data type comes with specific ranges of values and tasks that users can perform.
Primitive data types in programming languages are the language’s most essential types.
Primitive data types are directly supported by the hardware and have specific sizes and representations in memory. Primitive data are straightforward, meaning you cannot divide them into smaller pieces.
Primitive data types, unlike complex data types or objects, do not have methods or different properties associated with them. Primitive data types represent fundamental values such as characters, boolean values, or numbers.
Non-primitive data types in Java are data types that are not pre-defined. They are instead built by the programmer using classes or structures. Non-primitive data types in javascript have multiple values or components and can have complex systems.
Primitive data types in Java are fundamental data types that hold simple values and have fixed sizes, some of the primitive data types are stated below:
The boolean data type is commonly used in conditional statements to control the flow of the program based on specific conditions. It is also used in Boolean expressions and logical operations to compare and make decisions.
Here is an example:
public class BooleanExample {
public static void main(String[] args) {
boolean isSunny = true;
boolean isRaining = false;
System.out.println("Is it sunny? " + isSunny);
System.out.println("Is it raining? " + isRaining);
if (isSunny) {
System.out.println("Wear sunglasses!");
} else {
System.out.println("Carry an umbrella!");
}
}
}
The byte data type in Java is a primitive data type used to represent signed 8-bit integer values. It ranges from -128 to 127. The byte data type refers to when memory conservation is a priority or when working with raw binary data or streams.
public class ByteExample {
public static void main(String[] args) {
byte number = 100;
System.out.println("Number: " + number);
}
}
The primitive char data type represents a single 16-bit Unicode character. Char can store any character, including letters, digits, symbols, and spaces.
The short primitive data type represents signed 16-bit integer values. It stores whole numbers within the range of -32,768 to 32,767.
The short data type is practical in scenarios where memory conservation is a concern or when working with smaller integer values that fit within its range.
Int is a primitive data type that represents signed 32-bit integer values. Int ranges from -2,147,483,648 to 2,147,483,647 and is the most popular integer data type that is used in Java.
The long primitive data type represents signed 64-bit integer values and ranges from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.
It helps calculate large numbers or situations where the range of values exceeds the int data type limit.
The float data type is useful when precision is not the primary concern, and there is a need for a more comprehensive range of values with decimal points. It is suitable for scientific calculations, graphics, and audio processing scenarios.
The primitive double data type in Java represents double-precision 64-bit floating-point values. It can store decimal numbers more precisely than the float data type.
Double covers a range from 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative).
Non-primitive data types in Java include classes, arrays, interfaces, and more.
Some non-primitive data types examples are discussed below:
In Java, a class is a blueprint or template for creating objects. It defines the structure, behavior, and state of objects. A class serves as a blueprint from which users can make multiple objects.
Let us understand classes properly with this example:
In this example, we define a class called Person, which has a private instance variable name of type String. The class also has a constructor to initialize the name and a getter method to retrieve the name value.
Inside the main method, we create an instance of the Person class and pass the name "Oreo" as an argument to the constructor. Then, we use the getter method to retrieve the name and print it to the console.
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static void main(String[] args) {
Person person = new Person("Oreo");
System.out.println("Person name: " + person.getName());
}
}
An object is an instance of a class in Java. It represents a specific occurrence or realization of the class's structure and behavior. Objects have their own unique identity and can hold state and perform actions based on the defined behavior of the class.
Let us learn the function of objects with this example:
In this example, we declare a variable obj of type Object and assign it the value "Hello, World!".
The Object class is the root class in Java's class hierarchy, and it is a non-primitive data type that can hold references to any other type of object.
We use System.out.println() to print the value of obj to the console.
public class ObjectExample {
public static void main(String[] args) {
Object obj = "Hello, World!";
System.out.println(obj);
}
}
Java uses a String class to represent character strings. Java uses strings to store and process text-based data. Since strings cannot be muted, users cannot alter their values post-formation.
Here is an example of using strings in Java:
In this example, we declare a variable message of type String and assign it the value "Hello, World!".
public class StringExample {
public static void main(String[] args) {
String message = "Hello, World!";
System.out.println(message);
}
}
Arrays have a fixed size, and users can use an index to access any element. Arrays offer a practical way to manage and store collections of data.
Here is an example of using arrays in Java:
In this example, we declare an array variable numbers of type int[] and initialize it with the values {1, 2, 3, 4, 5}.
Arrays in Java are used to store multiple values of the same type in a contiguous memory block.
We use System.out.println() to print the length of the array (numbers.length), the first element of the array (numbers[0]), and the last element of the array (numbers[numbers.length - 1]) to the console.
The array numbers has a length of 5, and we access and print the first element (1) and the last element (5) of the array.
Arrays in Java are zero-indexed, so the first element is accessed using index 0, and the last element is accessed using length - 1.
public class ArrayExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
System.out.println("Array length: " + numbers.length);
System.out.println("First element: " + numbers[0]);
System.out.println("Last element: " + numbers[numbers.length - 1]);
}
}
An interface in Java is a collection of blueprints of linked methods that a class must install. It defines a collection of methods that users must provide a class before implementing the interface.
Here is an example of an interface in Java:
public interface Printable {
void print();
}
public class InterfaceExample {
public static void main(String[] args) {
Printable printable = new Document();
printable.print();
}
}
public class Document implements Printable {
@Override
public void print() {
System.out.println("Printing document...");
}
}
In order for this program to work, we must make three separate files for these three portions of code, Printable.java, InterfaceExample.java and Document.java.
In the above example, we defined an interface called Printable, which declares a single method print().
The Printable interface serves as a contract, specifying that any class that implements it must provide an implementation for the print() method.
We then created a class called Document that implements the Printable interface and provides its implementation for the print() method.
Inside the main method of the InterfaceExample class, we created an instance of the Document class and assigned it to a variable of type Printable. This demonstrates the usage of the interface as a non-primitive data type.
Finally, we invoked the print() method on the Printable object, which calls the implementation provided by the Document class.
Primitive and non-primitive data types differ based on operations, storage, and methods.
A few of the many differences between primitive and non-primitive data types in Java are stated in the table below.
Aspect | Primitive Data Types | Non-Primitive Data Types |
Definition | Basic data types provided by Java | Data types created by users |
Storage | Store actual values | Store references to objects |
Memory Size | Take up less memory | Take up more memory |
Default Value | Have default values | The default value is 'null.' |
Operations | Can perform arithmetic operations | Can perform complex operations |
Wrapper Classes | Have corresponding wrapper classes (e.g., Integer, Boolean) | No wrapper classes |
Mutability | Immutable | Mutable |
Methods | No methods associated with them | Can have methods and behaviors |
Examples | int, boolean, char | String, Object, Arrays, etc. |
Non-primitive data types in Java are not built into or specified by the language. These data types are user-created and are either derived from primitive data types or composite types.
Non-primitive data types in Java allow users to create sophisticated and customizable data structures and behaviors, giving the programming language flexibility and extensibility.
1. What are the examples of Java's non-primitive data types?
Strings, arrays, objects, and interfaces are some examples of non-primitive data types in Java.
2. What is the difference between primitive and non-primitive data types in Java?
Non-primitive data types are more complex in behavior and functions. Primitive data types are pre-defined, store actual values, and have a few functions.
3. What distinguishes a class from an object in Java?
A class serves as a model or template for building objects. An object is an instance of a class, signifying a detailed representation of the class’s structure and behavior.
PAVAN VADAPALLI
Director of Engineering
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...