In Java, data types define the type and size of values a variable can hold. They are divided into primitive and non-primitive categories. 𝐏𝐫𝐢𝐦𝐢𝐭𝐢𝐯𝐞 𝐃𝐚𝐭𝐚 𝐓𝐲𝐩𝐞𝐬: Primitive data types are built-in types that store simple values directly in memory. There are eight primitive data types: 𝐈𝐧𝐭𝐞𝐠𝐞𝐫 𝐓𝐲𝐩𝐞𝐬: byte (1 byte, −128 to 127) short (2 bytes, −32,768 to 32,767) int (4 bytes, most commonly used) long (8 bytes, for large integers) 𝐅𝐥𝐨𝐚𝐭𝐢𝐧𝐠 𝐏𝐨𝐢𝐧𝐭 𝐓𝐲𝐩𝐞𝐬: float (4 bytes, 6–7 decimal digits precision) double (8 bytes, 15–16 digits precision) 𝐂𝐡𝐚𝐫𝐚𝐜𝐭𝐞𝐫 𝐓𝐲𝐩𝐞: char (2 bytes, stores a single Unicode character like 'A' or '9') 𝐁𝐨𝐨𝐥𝐞𝐚𝐧 𝐓𝐲𝐩𝐞: Boolean (1 bit, stores true or false) These types are fast and memory-efficient, ideal for basic calculations and logic. 𝐍𝐨𝐧-𝐏𝐫𝐢𝐦𝐢𝐭𝐢𝐯𝐞 𝐃𝐚𝐭𝐚 𝐓𝐲𝐩𝐞𝐬: Non-primitive (reference) data types store references to memory locations rather than the actual values. 𝐂𝐥𝐚𝐬𝐬𝐞𝐬: Blueprints for creating objects containing fields and methods. 𝐀𝐫𝐫𝐚𝐲𝐬: Collections of elements of the same type stored in a sequence. 𝐒𝐭𝐫𝐢𝐧𝐠𝐬: Sequences of characters, implemented as objects in Java (𝐞.𝐠., "Hello"). These are user-defined or object types that are more flexible but memory costlier than primitives. #java #Day4 #Corejava Codegnan Support Team Codegnan Thanks to the mentor: Anand Kumar Buddarapu Saketh Kallepu Uppugundla Sairam
Java Data Types: Primitive and Non-Primitive
More Relevant Posts
-
Decoding Data: A Simple Guide to Java Data Types 📊 Understanding data types is foundational for writing effective and efficient Java code. They tell the compiler what kind of values a variable can hold and what operations can be performed on it. In Java, data types are broadly categorized into two groups: Primitive and Non-Primitive (Reference) types. Let's break them down! 1. Primitive Data Types (The Building Blocks) These are the most basic data types, directly supported by the language. They hold simple values and are always stored in memory where the variable is declared (on the stack). Numbers: byte, short, int, long: For whole numbers (integers) of increasing size. (int is the most commonly used!) float, double: For numbers with decimal points (floating-point numbers). (double is generally preferred for precision.) Characters: char: For single characters, like 'A', 'b', or '5'. Booleans: boolean: For true or false values. Essential for logic and control flow! 2. Non-Primitive Data Types (The References / Objects) Also known as "Reference Types," these are more complex. They don't store the actual values directly but rather references (memory addresses) to objects that store the data. They are created by the programmer or defined by Java. String: A sequence of characters (e.g., "Hello, World!"). While it looks like a primitive, String is actually a class in Java and thus a non-primitive type. Arrays: A collection of values of the same data type (e.g., int[] numbers = {1, 2, 3};). Classes & Interfaces: These are custom data types you define yourself (e.g., Car, Employee). They allow you to create complex objects that encapsulate data and behavior. Why does this matter? Choosing the right data type helps optimize memory usage, prevent errors, and ensures your program behaves as expected. For instance, using an int for an age makes more sense than a double. What data type do you find yourself using most often? Share your thoughts! #Java #Programming #DataTypes #SoftwareDevelopment #TechBasics #CodingTips #Developers
To view or add a comment, sign in
-
-
🔹 Variables in Java : A variable is a container that holds data which can be changed during program execution. Types of variables : 1) Local variable 2) Static variable 3) Instance variable Syntax: datatype variableName = value; Example : int age = 20; String name = "Guru"; double salary = 45000.50; 👉 Rules for variables: - Must start with a letter, $, or _ (not a digit). - Cannot use keywords (like class, int). - Java is case-sensitive (Age and age are different). 🔹 Data Types in Java : Java is a statically-typed language, so each variable must have a data type. 1. Primitive Data Types (8 types) : 👉 Data Type and Size : byte = 1 byte short = 2 byte int = 4 byte long = 8 byte float = 4 byte double = 8 byte char = 2 byte boolean = 1 bit 2. Non-Primitive Data Types : - String → "Hello Java" - Arrays → int[] arr = {1,2,3}; - Classes & Objects 🔹 Operators in Java : Operators are special symbols that perform operations on variables and values. 1. Arithmetic Operators : + Addition - Subtraction * Multiplication / Division % Modulus (remainder) 2. Relational (Comparison) Operators : == Equal to != Not equal to > Greater than < Less than >= Greater than or equal <= Less than or equal 3. Logical Operators : && Logical AND || Logical OR ! Logical NOT 4. Assignment Operators : = Assign += Add and assign -= Subtract and assign *= Multiply and assign /= Divide and assign %= Modulus and asshshift 5. Unary Operators : ++ Increment -- Decrement + Positive - Negative ! Logical NOT 6. Conditional (Ternary) Operator : variable = (condition) ? value_if_true : value_if_false; 7. Bitwise Operators : & AND | OR ^ XOR ~ NOT << Left shift >> Right shift >>> Unsigned right shift #Corejava #Java #learningjava #Programming #Coding #Fortunecloud #Cravita
To view or add a comment, sign in
-
-
In Java, when we want to work with a unique set of data, say String, the Set data structure comes readily to mind. There’s one subtle thing about the Set data structure that if care is not taken, might lead you to begin to debug an inexistent issue. One subtle thing that’s quick to forget about HashSet is that the order of insertion isn’t guaranteed. I was left chasing my tail recently only to remember the nature of HashSet in Java. Say we did the below: Set<String> names = Set.of(“Promise”, “Oberoro”, “Akeni”); If you: System.out.println(names); A couple of times, you would get different outcomes not consistent in the way you added the data. Same as using a HashSet and adding data to it. In my case, I wanted to use the Set in the order in which the data were inserted but while debugging, I would be seeing the last inserted data first 😄. I thought someone was jinxing me until I remembered that orderliness isn’t guaranteed in Set. Oops 😬 poor me! If you want orderliness and uniqueness then go for LinkedHashSet. Set<String> names = new LinkedHashSet<>(); names.add(“Promise”); names.add(“Oberoro”); names.add(“Akeni”); This way, the order of insertion will always be guaranteed and you don’t have to be fooling around like I did. But if order of insertion isn’t important to you then you can use HashSet without issues. The fact that the HashSet isn’t ordered is not a weakness, it’s a design decision made to optimize for speed, which makes insertion, deletion, and look up much faster than its counterparts(LinkedHashset and Treeset) as those would have the overheads of maintaining additional data structure. It’s also worth noting that HashSet under the hood uses Hash table, that’s how it’s able to achieve uniqueness. LinkedHashSet uses hash table + linked list. TreeSet uses Red-Black tree to ensure data are sorted in natural or specified order. Ever had the experience of debugging something that when you eventually figured it out, made you want to punch yourself in the face? Kindly share let’s learn from you 👇 #softwareengineering #programming #coding
To view or add a comment, sign in
-
-
☀️ Day 10: Arrays in Java Today’s focus was on Arrays — one of the most powerful tools to store and manage data efficiently in Java. 💡 What I Learned Today An array is a collection of similar data types stored in contiguous memory. Indexing starts from 0. Arrays have a fixed size once created. You can access elements easily using a loop. Arrays make data handling faster and organized. 🧩 Example Code public class ArrayExample { public static void main(String[] args) { int[] numbers = {10, 20, 30, 40, 50}; System.out.println("Array elements:"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } int sum = 0; for (int n : numbers) { sum += n; } System.out.println("Sum = " + sum); } } 🗣️ Caption for LinkedIn 📊 Day 10 – Arrays in Java Arrays are the backbone of structured data storage in Java. Today, I explored how to declare, access, and loop through arrays efficiently. Arrays make large data sets easy to manage — all stored neatly in one place! 💻 #CoreJava #LearnJava #Programming #JavaDeveloper #CodingJourney
To view or add a comment, sign in
-
-
🚀 𝗠𝗮𝘀𝘁𝗲𝗿𝗶𝗻𝗴 𝗝𝗮𝘃𝗮 𝗠𝗮𝗽 𝗜𝗻𝘁𝗲𝗿𝗳𝗮𝗰𝗲: 𝗛𝗮𝘀𝗵𝗠𝗮𝗽 𝘃𝘀 𝗟𝗶𝗻𝗸𝗲𝗱𝗛𝗮𝘀𝗵𝗠𝗮𝗽 𝘃𝘀 𝗧𝗿𝗲𝗲𝗠𝗮𝗽 𝘃𝘀 𝗛𝗮𝘀𝗵𝘁𝗮𝗯𝗹𝗲 If you’ve ever worked with key-value data in Java, the Map Interface is your go-to toolkit. It maps unique keys to specific values — just like storing names and phone numbers in your contacts list 📱. 𝗛𝗲𝗿𝗲’𝘀 𝗮 𝗾𝘂𝗶𝗰𝗸 𝗯𝗿𝗲𝗮𝗸𝗱𝗼𝘄𝗻: 🔹 𝗛𝗮𝘀𝗵𝗠𝗮𝗽: 𝘍𝘢𝘴𝘵𝘦𝘴𝘵, 𝘶𝘯𝘰𝘳𝘥𝘦𝘳𝘦𝘥, 𝘢𝘭𝘭𝘰𝘸𝘴 𝘰𝘯𝘦 𝘯𝘶𝘭𝘭 𝘬𝘦𝘺 — 𝘱𝘦𝘳𝘧𝘦𝘤𝘵 𝘧𝘰𝘳 𝘯𝘰𝘯-𝘵𝘩𝘳𝘦𝘢𝘥𝘦𝘥 𝘢𝘱𝘱𝘭𝘪𝘤𝘢𝘵𝘪𝘰𝘯𝘴. 🔹 𝗟𝗶𝗻𝗸𝗲𝗱𝗛𝗮𝘀𝗵𝗠𝗮𝗽: 𝘔𝘢𝘪𝘯𝘵𝘢𝘪𝘯𝘴 𝘪𝘯𝘴𝘦𝘳𝘵𝘪𝘰𝘯 𝘰𝘳𝘥𝘦𝘳 — 𝘨𝘳𝘦𝘢𝘵 𝘸𝘩𝘦𝘯 𝘰𝘳𝘥𝘦𝘳 𝘮𝘢𝘵𝘵𝘦𝘳𝘴. 🔹 𝗧𝗿𝗲𝗲𝗠𝗮𝗽: 𝘚𝘵𝘰𝘳𝘦𝘴 𝘬𝘦𝘺𝘴 𝘪𝘯 𝘴𝘰𝘳𝘵𝘦𝘥 (𝘢𝘴𝘤𝘦𝘯𝘥𝘪𝘯𝘨) 𝘰𝘳𝘥𝘦𝘳 — 𝘪𝘥𝘦𝘢𝘭 𝘧𝘰𝘳 𝘰𝘳𝘥𝘦𝘳𝘦𝘥 𝘥𝘢𝘵𝘢 𝘰𝘳 𝘳𝘢𝘯𝘨𝘦 𝘲𝘶𝘦𝘳𝘪𝘦𝘴. 🔹 𝗛𝗮𝘀𝗵𝘁𝗮𝗯𝗹𝗲: 𝘛𝘩𝘳𝘦𝘢𝘥-𝘴𝘢𝘧𝘦 𝘣𝘶𝘵 𝘰𝘭𝘥-𝘴𝘤𝘩𝘰𝘰𝘭 — 𝘳𝘦𝘱𝘭𝘢𝘤𝘦𝘥 𝘮𝘰𝘴𝘵𝘭𝘺 𝘣𝘺 𝘊𝘰𝘯𝘤𝘶𝘳𝘳𝘦𝘯𝘵𝘏𝘢𝘴𝘩𝘔𝘢𝘱. 𝗘𝘅𝗮𝗺𝗽𝗹𝗲: import java.util.*; public class MapExample { public static void main(String[] args) { // 𝟭️⃣ 𝗛𝗮𝘀𝗵𝗠𝗮𝗽 - 𝗨𝗻𝗼𝗿𝗱𝗲𝗿𝗲𝗱, 𝗮𝗹𝗹𝗼𝘄𝘀 𝗼𝗻𝗲 𝗻𝘂𝗹𝗹 𝗸𝗲𝘆 Map<Integer, String> hashMap = new HashMap<>(); hashMap.put(3, "Sachin"); hashMap.put(1, "Amit"); hashMap.put(2, "Ravi"); hashMap.put(null, "Unknown"); // Allowed System.out.println("HashMap: " + hashMap); // 𝟮️⃣ 𝗟𝗶𝗻𝗸𝗲𝗱𝗛𝗮𝘀𝗵𝗠𝗮𝗽 - 𝗠𝗮𝗶𝗻𝘁𝗮𝗶𝗻𝘀 𝗶𝗻𝘀𝗲𝗿𝘁𝗶𝗼𝗻 𝗼𝗿𝗱𝗲𝗿 Map<Integer, String> linkedHashMap = new LinkedHashMap<>(); linkedHashMap.put(3, "Sachin"); linkedHashMap.put(1, "Amit"); linkedHashMap.put(2, "Ravi"); System.out.println("LinkedHashMap: " + linkedHashMap); // 𝟯️⃣ 𝗧𝗿𝗲𝗲𝗠𝗮𝗽 - 𝗦𝗼𝗿𝘁𝗲𝗱 𝗯𝘆 𝗸𝗲𝘆, 𝗻𝗼 𝗻𝘂𝗹𝗹 𝗮𝗹𝗹𝗼𝘄𝗲𝗱 Map<Integer, String> treeMap = new TreeMap<>(); treeMap.put(3, "Sachin"); treeMap.put(1, "Amit"); treeMap.put(2, "Ravi"); System.out.println("TreeMap: " + treeMap); // 𝟰️⃣ 𝗛𝗮𝘀𝗵𝘁𝗮𝗯𝗹𝗲 - 𝗧𝗵𝗿𝗲𝗮𝗱-𝘀𝗮𝗳𝗲 𝗯𝘂𝘁 𝗻𝗼 𝗻𝘂𝗹𝗹 𝗮𝗹𝗹𝗼𝘄𝗲𝗱 Map<Integer, String> hashtable = new Hashtable<>(); hashtable.put(3, "Sachin"); hashtable.put(1, "Amit"); hashtable.put(2, "Ravi"); System.out.println("Hashtable: " + hashtable); // 🔍 𝗜𝘁𝗲𝗿𝗮𝘁𝗶𝗻𝗴 𝗼𝘃𝗲𝗿 𝗮 𝗠𝗮𝗽 for (Map.Entry<Integer, String> entry : hashMap.entrySet()) { System.out.println(entry.getKey() + " -> " + entry.getValue()); } } } 💡 𝗧𝗶𝗽: When you hear “𝗳𝗮𝘀𝘁 𝗹𝗼𝗼𝗸𝘂𝗽”, think HashMap. When you need 𝗼𝗿𝗱𝗲𝗿, think LinkedHashMap or TreeMap. When you need 𝘁𝗵𝗿𝗲𝗮𝗱 𝘀𝗮𝗳𝗲𝘁𝘆, go for ConcurrentHashMap. Understanding these differences helps you write cleaner, faster, and more scalable code 💪 #Java #HashMap #CollectionsFramework #LinkedHashMap #TreeMap #JavaInterviewPrep
To view or add a comment, sign in
-
🧩 1️⃣ Data Types in Java Java is a strongly typed language, meaning each variable must have a defined data type before use. There are two main categories: 🔹 Primitive Data Types: Used to store simple values like numbers, characters, or booleans. (Examples: int, float, char, boolean, etc.) 🔸 Non-Primitive Data Types: These store memory references rather than direct values. Includes Strings, Arrays, Classes, and Interfaces. Together, they define how data is represented and managed in memory. ⚙️ 2️⃣ Type Casting Type casting allows conversion from one data type to another. There are two kinds of casting in Java: ✅ Widening (Implicit) — Automatically converts smaller types to larger ones. 🧮 Narrowing (Explicit) — Manually converts larger types to smaller ones. This ensures flexibility while maintaining type safety, especially during calculations and data transformations. 🔄 3️⃣ Pass by Value vs Pass by Reference Java always uses Pass by Value, but the behavior varies depending on whether we’re working with primitives or objects. For Primitive Data Types: A copy of the value is passed, so changes inside the method don’t affect the original variable. For Objects (Reference Types): The reference (memory address) is passed by value, meaning both point to the same object. Any change made inside the method reflects on the original object. 💡 Key Takeaways ✅ Java has 8 primitive and multiple non-primitive data types. ✅ Type casting ensures smooth conversions between compatible types. ✅ Java is always pass-by-value, even when handling objects through references. 🎯 Reflection Today’s revision helped me understand how Java manages data behind the scenes — from defining variables to converting data types and managing memory references. Building strong fundamentals in these areas strengthens the base for advanced Java concepts ahead. 💪 #Java #Programming #Coding #FullStackDevelopment #LearningJourney #DailyLearning #RevisionDay #TAPAcademy #TechCommunity #SoftwareEngineering #JavaDeveloper #DataTypes #TypeCasting #PassByValue #PassByReference
To view or add a comment, sign in
-
-
🧠 What is hashCode in Java and when should you override it? In Java, every object has a hashCode() method inherited from Object. It returns an int value that acts like a numeric fingerprint of that object. It’s not guaranteed to be unique, but: • Equal objects must have the same hash code. • Different objects can have the same hash code (called a hash collision). Hash codes are mainly used to speed up lookups in hash-based data structures like HashMap, HashSet, and ConcurrentHashMap 🤔How does this lookup work? When you add or search for an object in a HashMap or HashSet, Java: 1. Calls hashCode() to find the bucket where the object might be. 2. Then uses equals() to check if the object is actually there. This makes lookups very fast on average O(1). Due to this double check, you must override hashCode() whenever you override equals(). This is required by the hashCode contract: “If two objects are equal according to equals(), then calling hashCode() on each of them must return the same integer result.” Failing to follow this rule can lead to: • Objects being “lost” in hash collections. • Inconsistent or unpredictable behavior. #Java #HashCode #Equals #JavaProgramming #SoftwareEngineering #DataStructures #CodingTips #HashMap #HashSet
To view or add a comment, sign in
-
-
☕ Day 9 of my “Java from Scratch” Series – “Data Types in Java” In Java, we should tell the compiler what type of data we want to store. There are 2 main types of Data Types 👇 🔹 1️⃣ Primitive Data Types These are the basic data types — the foundation of Java. There are 8 primitive data types: 1. byte - 1 byte (1 byte = 8bits) 2. short - 2 bytes 3. int - 4 bytes 4. long - 8 bytes 5. float - 4 bytes 6. double - 8 bytes 7. char - 2 bytes 8. boolean - 1 bit 💡 In Java, everything is a class except these 8 data types. 🔹 2️⃣ Non-Primitive Data Types These are user-defined data types, and they are classes. ✅ Examples: String, Array The size of a String depends on the number of characters in it. Example: String name = "Java"; 👉 Number of characters = 4 👉 Size of each character = 2 bytes ✅ Total = 8 bytes + overhead (20–30 bytes depending on the system) 💡 Key takeaway: ➡️ Primitive data types ❌ are not classes. ➡️ Non-primitive data types ✅ are classes. 👉 Which data type do you use most often in your projects? Let me know in the comments 👇 #Java #Programming #Coding #Learning #SoftwareEngineering #JavaDeveloper #DataTypes #JavaFromScratch #InterviewQuestions #DataTypesInJava #Tech #JavaInterviewTopics #NeverGiveUp
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development