Day 13 : Variables 1. Variables are the containers in which we store data. 2. In java we can create variables with the help of datatype. 3.To give the name of variable should follow the Camel Case convention. Variable declaration and initialization statement: - 1.Declare a variable and store the value simultaneously. Syntax:- datatype variablename = value / expression ; E.g. :- int num=100; String name = “Shubham”; ⚠️ We cannot use a variable without declaring it. Variables are classified into two types 1. Class level Variables/Instance Variable:- The variables which are declared in the class block are known as Class level variable or Instance Variable. We can use a Instance variable without initialization because Instance variables Default values are assigned by the JVM. # In Java There is no Concept called Global Variable. They are classified into two types, i. Static Variable ii. Non-static Variable Examples: 1. class Demo { String name = "Ajit Pawar"; // instance variable main(String[] args) { Demo d = new Demo(); System.out.println(d.name); } } 2. class Demo{ static int a=10; //static variable main(){ s.o.p(a); // 10 } } 2. Local Variables:- The variables which are declared in the method block or any other block other than class block is known as local variables. For Ex. In Method block , constructor block, control flow statements block etc. Examples. 1. class Example{ main(){ int a =10; s.o.p(a); // 10 } } 2. class Example{ main(){ { int a =10; } s.o.p(a); // CTE } } #Java #CoreJava #JavaProgramming #JavaVariables #LearnJava #CodingJourney #ProgrammingStudent #DailyLearning #ComputerScience
Java Variables: Declaration, Initialization, and Types
More Relevant Posts
-
Java Array A data structure that stores multiple values of the same data type in a single variable. Arrays Class A utility class in Java that provides methods to perform operations on arrays such as sorting and searching. String A class used to represent a sequence of characters in Java; String objects are immutable. String Methods Predefined methods used to manipulate and retrieve information from String objects. StringBuffer and StringBuilder Classes used to create mutable strings; StringBuffer is thread-safe, while StringBuilder is faster but not thread-safe. Immutable and Mutable Immutable objects cannot be changed after creation, while mutable objects can be modified. Exception Handling A mechanism to handle runtime errors and maintain the normal flow of program execution. Shallow Copy and Deep Copy Shallow copy copies object references, while deep copy creates a new independent object. File Handling A process of creating, reading, writing, and deleting files in Java. Multithreading A feature that allows multiple threads to execute concurrently within a program. Synchronization A mechanism used to control access to shared resources in a multithreaded environment. Interthread Communication A technique that allows threads to communicate with each other during execution. Deadlock A situation where two or more threads wait indefinitely for each other’s resources. Daemon Thread A background thread that runs to support user threads and terminates when they finish. Inner Class A class defined inside another class to logically group related classes. Object Class The root superclass of all Java classes from which every class implicitly inherits. pdf Link :- https://lnkd.in/gXEWSAJ2 #Java #CoreJava #JavaDeveloper #JavaInterview #ProgrammingBasics #SoftwareDevelopment #LearnJava #StudentDeveloper #TechLearning
To view or add a comment, sign in
-
-
Day 15 - 🚀Arrays in Java An array in Java is a data structure used to store multiple values of the same data type in a single variable. Arrays help manage large amounts of data efficiently and make code more organized and readable. 🔹 Why Use Arrays? Store multiple values in one variable Fast access using index Improves code clarity Reduces memory overhead Essential for data handling and algorithms 🔹 Key Features of Arrays in Java Fixed size (defined at creation) Zero-based indexing Can store primitive & non-primitive data types Stored in contiguous memory locations 🔹 Types of Arrays in Java 1️⃣ One-Dimensional Array Used to store a list of values. int[] numbers = {10, 20, 30, 40}; 2️⃣ Two-Dimensional Array Used to store data in rows and columns. int[][] matrix = { {1, 2}, {3, 4} }; 3️⃣ Multi-Dimensional Array Array of arrays (more than two dimensions). int[][][] data = new int[2][3][4]; 🔹 Accessing Array Elements int value = numbers[0]; 🔹 Advantages of Arrays ✔ Easy data access ✔ Efficient memory usage ✔ Simplifies repetitive data storage 🔹 Limitations of Arrays ✖ Fixed size ✖ Stores only same data type ✖ No built-in methods for dynamic operations 🚀 Conclusion Arrays form the foundation of data structures in Java. Mastering arrays is crucial for understanding advanced concepts like lists, stacks, queues, and algorithms. #Java #ArraysInJava #JavaProgramming #DataStructures #CodingBasics #StudentDeveloper #LearnJava #Programming
To view or add a comment, sign in
-
-
🚀 Java Array Cheat Sheet — Quick Revision Guide Arrays are one of the most fundamental data structures in Java. Here’s a quick overview every Java developer should know. ✅ What is an Array? An array is a fixed-size, index-based data structure that stores elements of the same data type. Examples: int[] a = new int[10]; // Array of 10 integers char[] c = new char[15]; // Array of 15 characters String[] s = new String[20]; // Array of 20 strings ✅ Array Structure 🔹Java arrays use zero-based indexing: 🔹First element → index 0 🔹Second element → index 1 and so on. int[] arr = {21, 15, 37, 53, 17}; ✅ Types of Arrays 🔹 Single Dimensional Array – One row of elements 🔹 Multidimensional Array – Array of arrays 2D Array 3D Array 🔹 Jagged Array (rows with different lengths) ✅ Array Declaration int[] arr; int arr[]; ✅ Array Initialization int[] arr = new int[5]; arr[0] = 21; ✅ Array Traversal for(int i=0; i<arr.length; i++){ System.out.println(arr[i]); } ✅ Useful Arrays Class Methods 🔹sort() → Sort array 🔹stream() → Convert to stream 🔹fill() → Fill values 🔹copyOf() → Copy array 🔹binarySearch() → Search element asList() → Convert array to list ✅ Advantages ☑️ Easy to use ☑️ Fast data access ☑️ Supports primitive & object types ❌ Limitations ✖ Fixed size ✖ No built-in dynamic resizing 💡 Arrays are the foundation for advanced data structures like Lists, Stacks, and Queues — master them first! #Java #Programming #JavaDeveloper #Parmeshwarmetkar #Coding #DataStructures #LearnJava
To view or add a comment, sign in
-
-
Hi Connections 👋 Today, I learned about Strings in Java and the various methods used to manipulate data. 📌 Definition: A String is a group or sequence of characters. It is immutable, which means once it is created, it cannot be changed. 📌 Commonly Used String Methods with Definitions: 🔹 length() – Returns the number of characters present in the string. 🔹 charAt() – Returns the character at a specified index position. 🔹 toUpperCase() – Converts all characters in the string to uppercase. 🔹 toLowerCase() – Converts all characters in the string to lowercase. 🔹 split() – Splits the string into multiple parts based on a specified delimiter. 🔹 indexOf() – Returns the index position of the first occurrence of a specified character or substring. 🔹 lastIndexOf() – Returns the index position of the last occurrence of a specified character or substring. 🔹 substring() – Extracts a portion of the string from a given index. 🔹 valueOf() – Converts different data types into string format. 🔹 trim() – Removes leading and trailing white spaces from the string. 🔹 contains() – Checks whether the string contains a specific sequence of characters. 🔹 equals() – Compares two strings and checks if they are equal. 🔹 equalsIgnoreCase() – Compares two strings by ignoring case sensitivity. 🔹 replace() – Replaces a specified character or substring with another. 🔹 startsWith() – Checks if the string starts with a specified prefix. 🔹 endsWith() – Checks if the string ends with a specified suffix. 🔹 isEmpty() – Checks whether the string is empty or not. 🔹 concat() – Joins two strings together. Understanding these methods helps in performing various data manipulation operations efficiently while working with text-based data. #Java #Programming #LearningJourney #Coding
To view or add a comment, sign in
-
-
🧠 var Keyword in Java — Type Inference Explained #️⃣ var keyword in Java Introduced in Java 10, var allows local variable type inference. 👉 The compiler automatically determines the variable type. 🔹 What is var? var lets Java infer the type from the assigned value. Instead of writing: String name = "Vijay"; You can write: var name = "Vijay"; The compiler still treats it as String. 👉 var is NOT dynamic typing 👉 Type is decided at compile-time 🔹 Why was var introduced? Before var: ⚠ Long generic types ⚠ Repeated type declarations ⚠ Verbose code ⚠ Reduced readability Example: Map<String, List<Integer>> data = new HashMap<>(); With var: var data = new HashMap<String, List<Integer>>(); Cleaner and easier to read. 🔹 Rules of using var ✔ Must initialize immediately ✔ Only for local variables ✔ Cannot use without value ✔ Cannot use for fields or method parameters ✔ Type cannot change after assignment Invalid: var x; // ❌ compile error 🔹 Where should we use var? Use var when: ✔ Type is obvious from right side ✔ Long generic types ✔ Stream operations ✔ Loop variables ✔ Temporary variables Avoid when: ❌ It reduces readability ❌ Type becomes unclear 🧩 Real-world examples var list = List.of(1, 2, 3); var stream = list.stream(); var result = stream.map(x -> x * 2).toList(); Perfect for modern functional style. 🎯 Interview Tip If interviewer asks: Is var dynamically typed? Answer: 👉 No. Java is still statically typed. 👉 var only removes repetition. 👉 Type is fixed at compile-time. 🏁 Key Takeaways ✔ Introduced in Java 10 ✔ Local variable type inference ✔ Reduces boilerplate ✔ Improves readability ✔ Not dynamic typing ✔ Use wisely #Java #Java10 #VarKeyword #ModernJava #JavaFeatures #CleanCode #ProgrammingConcepts #BackendDevelopment #JavaDeepDive #TechWithVijay #VFN #vijayfullstacknews
To view or add a comment, sign in
-
-
Day 3: Today’s practice: Java basics 💻 Worked on data types, literals, and user input handling. Understanding fundamentals deeply because strong basics create strong developers. •Literals can be written in the form: byte age = 34; int age2 = 56; short age3 = 87; long ageDino = 5666666L; char ch = 'A'; float f1 = 5.6f; double d1 = 4.66d; boolean a = true; string str = "Mohan"; •Taking input values from keyboard: import java.util.Scanner; → It is used for while using input values from keyboard. public class java-input.code { static void main (String[] args) { s.o.ut (" Taking input numbers"); Scanner mn = new Scanner (System.in); → (Scanner class object) → Declare it first before entering any input. s.o.ut (" Enter any number : "); int a = mn.nextInt(); → call it after a line to give an input. s.o.ut (" Enter any number : "); int b = mn.nextInt(); int sum = a + b; s.o.ut (" The sum of two number : "); s.o.ut (sum); } } •To take input from the user if we need a complete line we use: string str = sc.nextLine(); s.out(str); •Input can be checked using the below method for which literal it belongs to: boolean b1 = sc.hasNextInt(); s.o.ut (b1);
To view or add a comment, sign in
-
-
✅ Variables in Java 🔹 What is a Variable? A variable is a named memory location used to store data. Example: int age = 20; Here: age → Variable name int → Data type 20 → Value stored ✅ Syntax of Variable dataType variableName = value; Example: double salary = 5000.50; ✅ Types of Variables in Java Java has 3 main types: 1️⃣ Local Variable 2️⃣ Instance Variable 3️⃣ Static Variable 1️⃣ Local Variable Declared inside a method. Accessible only inside that method. Must be initialized before use. Example: class Demo { public static void main(String[] args) { int number = 10; // Local variable System.out.println(number); } } ✔ Scope → Only inside main() ✔ Stored in Stack memory 2️⃣ Instance Variable Declared inside class but outside method. Belongs to object. Each object gets its own copy. Example: class Student { int age; // Instance variable } Usage: Student s1 = new Student(); s1.age = 20; ✔ Stored in Heap memory ✔ Requires object to access 3️⃣ Static Variable Declared with static keyword. Belongs to class. Shared among all objects. Example: class Student { static String schoolName = "ABC School"; } Access: System.out.println(Student.schoolName); ✔ Only one copy exists ✔ Shared by all objects 🎯 Example Using All Three class Demo { int instanceVar = 10; // Instance static int staticVar = 20; // Static public static void main(String[] args) { int localVar = 30; // Local Demo obj = new Demo(); System.out.println(obj.instanceVar); System.out.println(staticVar); System.out.println(localVar); } }
To view or add a comment, sign in
-
-
Day 9- What I Learned In a Day(JAVA) What is a Data Type? A data type in Java defines the type of value a variable can store and how much memory is allocated for it. Types of Data Types in Java: Java data types are divided into two categories: 1️⃣ Primitive Data Types Primitive data types store simple values directly in memory. They are predefined by Java and have fixed size. Java has 8 primitive data types: 1️⃣ byte Size: 1 byte (8 bits) Range: -128 to 127 2️⃣ short Size: 2 bytes (16 bits) Range: -32,768 to 32,767 3️⃣ int Size: 4 bytes (32 bits) Range: -2,147,483,648 to 2,147,483,647 4️⃣ long Size: 8 bytes (64 bits) Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 5️⃣ float Size: 4 bytes (32 bits) Range: Approximately ±3.4 × 10³⁸ 6️⃣ double Size: 8 bytes (64 bits) Range: Approximately ±1.7 × 10³⁰⁸ 7️⃣ char Size: 2 bytes (16 bits) Range: 0 to 65,535 (Unicode characters) 8️⃣ boolean Size: JVM dependent (typically 1 bit logically) Values: true or false Note:The number datatype in increasing order the capacity byte<short<int<long<float<double Non-Primitive Data Types: Non-primitive data types store references (addresses) of objects, not the actual value. They do not have fixed size. Examples: String Arrays Classes Objects Interfaces Note:In java,every class name is considered as a Non-Primitive Datatype. Practiced the primitive datatype declaration: 👇 #Java #JavaProgramming #CoreJava #LearnJava #CodingJourney #DailyLearning
To view or add a comment, sign in
-
🔷 TreeSet in Java – 📌 1. What is TreeSet? TreeSet is a class in Java that implements the NavigableSet (SortedSet) interface. It stores unique elements only and maintains them in sorted (ascending) order by default. 👉 No duplicates 👉 Automatically sorted Internally, TreeSet uses a Red-Black Tree (self-balancing binary search tree). ⚙ 2. Internal Data Structure of TreeSet When you add an element: set.add("Java"); Internally: 👉 Element is placed in a Red-Black Tree 👉 Tree keeps elements in sorted order 👉 Tree remains balanced for performance 🧠 Structure Used: • Red-Black Tree • Self-balancing BST This guarantees: ✅ Sorted data ✅ Good performance 🧱 4. Constructors of TreeSet 1️⃣ Default Constructor TreeSet<String> set = new TreeSet<>(); • Natural sorting order 2️⃣ With Comparator (custom sorting) TreeSet<String> set = new TreeSet<>(Collections.reverseOrder()); • Descending order 3️⃣ With Collection TreeSet<String> set = new TreeSet<>(list); • Removes duplicates • Sorts automatically ⭐ 5. Important Characteristics ✔ No duplicate elements ✔ Always sorted ✔ Does NOT allow null (Java 8+) ✔ Not synchronized ✔ Slower than HashSet & LinkedHashSet TAP Academy , Rohit Ravinder , Somanna M G , Sharath R , Ravi Magadum , kshitij kenganavar , Poovizhi VP , Hemanth Reddy #Java #TreeSet #JavaCollections #CoreJava #JavaDeveloper #Programming #Coding #SoftwareDevelopment #TechLearning #DataStructures #LearningJava #DeveloperCommunity
To view or add a comment, sign in
-
-
🛑 A Quick Java Memory Guide Understanding the Java Memory Model is non-negotiable for writing performant, bug-free code. If you don’t know where your data lives, you don’t really know Java. Here is the breakdown of Stack vs Heap: 🧠 The Stack (LIFO - Last In, First Out) · What lives here: Primitive values (int, double) and references to objects (pointers). · Scope: Each thread has its own private stack. Variables exist only as long as their method is running. · Access Speed: Very fast. · Management: Automatically freed when methods finish. 🗄️ The Heap (The Common Storage) · What lives here: The actual objects themselves (all instances of classes). · Scope: Shared across the entire application. · Access Speed: Slower than the stack due to global access. · Management: Managed by the Garbage Collector (GC). 💡 The Golden Rule: The reference is on the Stack, but the object it points to is on the Heap. Map<String, String> myMap = new HashMap<>(); (Stack: myMap) --> (Heap: HashMap object) 👇 Q1: If I declare int id = 5; inside a method, where is this value stored? A1: Stack. It's a local primitive variable, so it lives directly in the stack frame. Q2: I created an array: int[] data = new int[100];. The array holds primitives. Is the data stored on the Stack or Heap? A2: Heap. The array itself is an object in Java. The reference data lives on the Stack, but the 100 integers are stored contiguously on the Heap. Q3: What happens to memory if I pass this object reference to another method? A3: A copy of the reference is passed (passed by value). Both methods now have a pointer (on their respective stacks) to the same single object on the Heap. ♻️ Repost if you found this helpful! Follow me for more Java wisdom. #Java #Programming #SoftwareEngineering #MemoryManagement #Coding
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