🔹 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
Java Variables, Data Types, and Operators Explained
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
-
-
🧩 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
-
-
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
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
-
☀️ 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
-
-
📌Understanding Java Arrays — The Foundation of Data Handling Today, I revised the fundamentals of Java Arrays, one of the most essential concepts in Java programming and interviews. 🔹 What is an Array in Java? A Java array is a fixed-size, indexed data structure used to store multiple values of the same data type. Arrays are stored in continuous memory locations and allow fast (O(1)) element access. ➜ Example: int[] marks = new int[10]; char[] letters = new char[15]; String[] names = new String[20]; 🔹 Array Structure Arrays use zero-based indexing Each element is stored at a specific index Accessing elements is extremely fast ➜Example: int[] arr = {21, 15, 37, 53, 17}; Memory view: Index: 0 1 2 3 4 Values: 21 15 37 53 17 🔹 Array Declaration (Two Ways) int[] arr; int arr[]; 🔹 Types of Arrays in Java ✔ 1. One-Dimensional Arrays Ideal for simple linear data: int[] scores = {10, 20, 30}; ✔ 2. Multidimensional Arrays Arrays inside arrays → used for matrices, tables, grids. 2D Array: int[][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; 3D Array: Used in simulations, 3D structures, games: int[][][] cube = new int[3][3][3]; ✔ 3. Jagged Arrays (Irregular Arrays) Rows can have different lengths. int[][] jagged = { {1, 2}, {3, 4, 5}, {6, 7, 8, 9} }; 🔹 Why Arrays Matter? Foundation for Data Structures (Lists, Maps, Matrices) Faster access compared to collections Used in interviews for logic & memory questions Understanding arrays is the first step toward mastering Java data structures. #Java #Programming #Arrays #DSA #BackendDevelopment #LearningInPublic #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
-
💡 Array vs Arrays in Java — What’s the real difference? Many developers mix up these two, but they play very different roles! Array: The Data Container Used to store multiple values of the same type (like numbers, strings, etc.) Just holds the data; doesn’t have built-in methods for sorting or searching Example: int[] numbers = {3, 1, 4, 1, 5}; System.out.println(numbers[0]); // Output: 3 Arrays: The Utility Toolbox Part of java.util; helps you sort, search, and print arrays with static methods Makes array manipulation super easy! Example: import java.util.Arrays; int[] numbers = {3, 1, 4, 1, 5}; Arrays.sort(numbers); System.out.println(Arrays.toString(numbers)); // Output: [1, 1, 3, 4, 5] Summary: Array = the box that stores items Arrays = the set of tools you use to organize those items 🔎 Did you know? There’s also a hidden Array class in java.lang.reflect! It’s used for advanced stuff like creating or managing arrays dynamically. Example: import java.lang.reflect.Array; Object array = Array.newInstance(String.class, 3); Array.set(array, 0, "Java"); Array.set(array, 1, "Python"); Array.set(array, 2, "C++"); System.out.println(Array.get(array, 1)); // Output: Python Usually, you’ll work with int[], String[], and the Arrays class for everyday coding. The reflection-based Array class works behind the scenes! #Java #ArrayVsArrays #ProgrammingTips #Learning #CodeTips
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