Built-in Method Overloading in Java 1️⃣ System.out.println() Overloaded to print different data types such as int, double, String, char, boolean, and Object. 2️⃣ Math class methods Methods like Math.max() and Math.min() are overloaded to work with int, long, float, and double values. 3️⃣ Other overloaded built-in methods Methods such as String.valueOf() and Arrays.sort() are overloaded to handle different data types and parameter combinations.
Java Overloading: Built-in Methods for Various Data Types
More Relevant Posts
-
𝐃𝐚𝐲 𝟗 – 𝐑𝐞𝐯𝐢𝐬𝐢𝐭𝐢𝐧𝐠 𝟐-𝐃 𝐀𝐫𝐫𝐚𝐲𝐬 𝐢𝐧 𝐉𝐚𝐯𝐚 One important realization: 👉 In Java, a 2-D array is NOT actually a “matrix”. It is an array of arrays. That means: Each row is a separate array object. Rows can have different lengths. Memory is not necessarily continuous like in C/C++ Example: int[][] arr = new int[3][]; arr[0] = new int[2]; arr[1] = new int[4]; arr[2] = new int[3]; This is called a 𝑱𝒂𝒈𝒈𝒆𝒅 𝑨𝒓𝒓𝒂𝒚. Many beginners assume: arr.length == total elements ❌ But actually: arr.length → number of rows arr[i].length → number of columns in that specific row This small understanding prevents many traversal bugs. The deeper you understand memory structure, the cleaner your logic becomes. 🔹 Beginner Level 2-D Array Problems : These are simple but build strong fundamentals. 1️⃣ Print a 2D array----> Traverse and print all elements. 2️⃣ Find the sum of all elements 3️⃣ Row-wise sum-----> Print sum of each row separately. 4️⃣ Column-wise sum---->This helps understand traversal direction change. 5️⃣ Find the largest element----> Simple scanning problem. #Java #DataStructures #LearningInPublic #dailyChallenge
To view or add a comment, sign in
-
Some Java features look simple on the surface… until you dive into generics and realize how much control they actually give you over type safety and structure. Once you understand how bounds, wildcards, and inference interact, a lot of the “rules” in the language start to click into place. If you’ve ever wondered why certain generic designs work beautifully and others fall apart, this article reveals the patterns underneath. https://bit.ly/4aeD3DM
To view or add a comment, sign in
-
Once you understand classes and encapsulation, the next question becomes: How do we reuse behavior properly? That’s where 𝗶𝗻𝗵𝗲𝗿𝗶𝘁𝗮𝗻𝗰𝗲 comes in. Instead of rewriting common logic, Java allows one class to extend another. Example: 𝚌𝚕𝚊𝚜𝚜 𝚅𝚎𝚑𝚒𝚌𝚕𝚎 { 𝚟𝚘𝚒𝚍 𝚜𝚝𝚊𝚛𝚝() { 𝚂𝚢𝚜𝚝𝚎𝚖.𝚘𝚞𝚝.𝚙𝚛𝚒𝚗𝚝𝚕𝚗("𝚅𝚎𝚑𝚒𝚌𝚕𝚎 𝚜𝚝𝚊𝚛𝚝𝚒𝚗𝚐"); } } 𝚌𝚕𝚊𝚜𝚜 𝙲𝚊𝚛 𝚎𝚡𝚝𝚎𝚗𝚍𝚜 𝚅𝚎𝚑𝚒𝚌𝚕𝚎 { 𝚟𝚘𝚒𝚍 𝚑𝚘𝚗𝚔() { 𝚂𝚢𝚜𝚝𝚎𝚖.𝚘𝚞𝚝.𝚙𝚛𝚒𝚗𝚝𝚕𝚗("𝙱𝚎𝚎𝚙"); } } Now 𝐂𝐚𝐫 automatically gets the 𝐬𝐭𝐚𝐫𝐭() behavior. This is powerful. But inheritance is not about copying code. It’s about expressing relationships. A 𝗖𝗮𝗿 𝗶𝘀 𝗮 𝗩𝗲𝗵𝗶𝗰𝗹𝗲. That phrase matters. Inheritance should represent: • Logical hierarchy • Shared behavior • Clear relationships When misused, it creates tight coupling and fragile systems. Today was about: • Understanding 𝗲𝘅𝘁𝗲𝗻𝗱𝘀 • The “is-a” relationship • When inheritance makes sense (and when it doesn’t) Reuse is important. But correct modeling is more important. #Java #OOP #Inheritance #SoftwareDesign #CleanCode #LearningInPublic
To view or add a comment, sign in
-
-
Quick Sort in Java may look complex, but most mistakes happen because: • we don’t clearly understand how the pivot divides the array • we mix up the partition logic • we think recursion is doing the sorting The core idea is straightforward: choose a pivot, place it in the correct position, and recursively apply the same process to the left and right parts. What really happens: – A pivot element is selected – The array is rearranged so that elements smaller than pivot go to the left – Elements greater than pivot go to the right – The pivot lands at its final sorted position Then: – The left subarray is partitioned again using a new pivot – The right subarray is partitioned again – This continues until subarrays have 0 or 1 element – A single element is already sorted by definition So: – First, one pivot divides the array into two parts – Then each part is divided again – Then those smaller parts are divided further – And so on, until everything is positioned correctly The key insight: 👉 All the real sorting happens during partitioning, not after recursion finishes. Once you understand that: • recursion just keeps reducing the problem size • partition logic ensures correct positioning • average time complexity stays O(n log n) Quick Sort isn’t about guaranteed worst-case performance — it’s about speed, in-place efficiency, and practical performance. That’s what makes it a foundation algorithm for: ✔ in-memory sorting ✔ competitive programming ✔ high-performance systems #Java #QuickSort #DSA #Algorithms #DivideAndConquer #ProblemSolving #BackendEngineering
To view or add a comment, sign in
-
Problem Statement: Write a function that reverses a string. The input string is given as an array of characters s. You must do this by modifying the input array in-place with O(1) extra memory. (LeetCode 344) Solution: So the approach I have applied in this problem is very very simple. We initialize a pointer start with 0 and another pointer end with ch.length-1, the start pointer points to first position of the array at the beginning and the end pointer points to the end of the array at the beginning . We swap the elements at start position with the element at end position and then we do start++ and end-- to move the start pointer to the next position and end pointer to the previous position (the second last position in the second iteration, third last in the third iteration and so on). We continue iteration until the condition (start <= end) fails. This simply reverse the array with character elements. Time Complexity : O(n) Space Complexity: O(1) Below is the Java Code for the following problem: public static void reverse(char []ch) { int start = 0; int end = ch.length-1; while(start <= end) { char temp = ch[start]; ch[start] = ch[end]; ch[end] = temp; start ++; end --; } }
To view or add a comment, sign in
-
👉 Write a Java program to check if a number is prime. 🧠 Logic explanation: if(number <= 1) //prime number: must be greater than 1 exactly 2 factors 1 and itself. for(int i = 2;i<= Math.sqrt(number);i++) /*This loop checks if any number from 2 up to the square root of the number can divide it. example: 1 × 36 2 × 18 3 × 12 4 × 9 6 × 6 After 6, the pairs start repeating in reverse*/
To view or add a comment, sign in
-
-
📌 Optimized Prime Number Check in Java (Time Complexity O(√n)) While practicing problem-solving, I implemented an optimized Prime Number check using mathematical reasoning instead of brute force. Instead of checking divisibility from 2 to n-1 (O(n)), I reduced the complexity to O(√n). 🔍 Key Optimizations Used: 1️⃣ Handle edge cases separately n == 1 → Not prime n == 2 or n == 3 → Prime 2️⃣ Eliminate obvious non-primes early If n % 2 == 0 or n % 3 == 0 → Not prime 3️⃣ Check only up to √n If a number n = a × b, then at least one of a or b must be ≤ √n. This reduces unnecessary computations significantly. ⏱ Complexity Analysis: • Time Complexity → O(√n) • Space Complexity → O(1) 💡 Why This Matters? Optimizing from O(n) to O(√n) shows: Strong mathematical thinking Better algorithm design Interview-level optimization skills Small improvements in complexity can make a huge difference for large inputs. #Java #DataStructures #Algorithms #ProblemSolving #TimeComplexity #CodingInterview #LearningJourney
To view or add a comment, sign in
-
-
🚀 Prefix Sum — Answer Range Queries in O(1) While practicing DSA, I revisited one of the simplest yet most powerful techniques — Prefix Sum. 👉 It helps compute range sums efficiently after one preprocessing pass. --- 💡 What is Prefix Sum? A prefix sum array stores the cumulative sum up to each index. Formula: prefix[i] = prefix[i-1] + arr[i] --- 📌 Example Input Array: arr = [2, 4, 6, 8, 10] Prefix Array becomes: prefix = [2, 6, 12, 20, 30] --- 🎯 Range Sum Query (L = 1, R = 3) Instead of looping every time: sum(L, R) = prefix[R] − prefix[L−1] = 20 − 2 = 18 ⚡ Preprocessing → O(n) ⚡ Each query → O(1) --- 🔥 Where is it useful? ✅ Range sum queries ✅ Subarray sum problems ✅ Count subarrays with given sum ✅ 2D prefix sum problems ✅ Competitive programming --- 💻 Java Implementation int[] prefix = new int[n]; prefix[0] = arr[0]; for(int i = 1; i < n; i++){ prefix[i] = prefix[i-1] + arr[i]; } // range sum from L to R int rangeSum = prefix[R] - (L > 0 ? prefix[L-1] : 0); --- ✨ Preprocess once, answer many queries fast! #DSA #PrefixSum #Coding #Java #CompetitiveProgramming #WomenInTech
To view or add a comment, sign in
-
-
𝐓𝐡𝐢𝐬 𝐜𝐨𝐝𝐞 𝐬𝐧𝐢𝐩𝐩𝐞𝐭 𝐢𝐬 𝐞𝐧𝐨𝐮𝐠𝐡 𝐭𝐨 𝐜𝐨𝐧𝐟𝐮𝐬𝐞 𝐬𝐨𝐦𝐞𝐨𝐧𝐞 𝐰𝐢𝐭𝐡 𝐉𝐚𝐯𝐚. 😁 Let me introduce you to the concept of wrapper classes and autoboxing first. 𝐖𝐑𝐀𝐏𝐏𝐄𝐑 𝐂𝐋𝐀𝐒𝐒𝐄𝐒: These are classes used to wrap primitive values so we can treat them like objects. This is essential for the Collection framework (like ArrayList), which only works with objects. 𝐀𝐔𝐓𝐎𝐁𝐎𝐗𝐈𝐍𝐆: The process of converting primitive values to wrapper objects is implicit. We don't need the new keyword. Now let's discuss the code: 𝐂𝐎𝐃𝐄 1: 𝘪𝘯𝘵 𝘢 = 100; 𝘪𝘯𝘵 𝘣 = 100; These are primitive values. For primitives, the == operator compares the actual values. Since both are 100, the output is 𝐭𝐫𝐮𝐞. 𝐂𝐎𝐃𝐄 2: Integer wrapper classes have an Integer Cache. By default, Java caches all Integer objects in the range of -128 to 127. 𝘐𝘯𝘵𝘦𝘨𝘦𝘳 𝘱 = 100; 𝘐𝘯𝘵𝘦𝘨𝘦𝘳 𝘲 = 100; Since 100 is in the cache, Java points both variables to the same object memory location. The output is 𝐭𝐫𝐮𝐞. 𝐂𝐎𝐃𝐄 3: 𝘐𝘯𝘵𝘦𝘨𝘦𝘳 𝘺 = 128; 𝘐𝘯𝘵𝘦𝘨𝘦𝘳 𝘻 = 128; Because 128 is outside the cache range, Java creates two distinct objects in memory. Since == compares memory references for objects, the output is 𝐟𝐚𝐥𝐬𝐞. ~Anuprash Gautam 🍵 𝘒𝘦𝘦𝘱 𝘭𝘦𝘢𝘳𝘯𝘪𝘯𝘨 𝘢𝘯𝘥 𝘣𝘳𝘦𝘸𝘪𝘯𝘨 𝘤𝘰𝘧𝘧𝘦𝘦.😊 #Java #programming #autoboxing #oops
To view or add a comment, sign in
-
-
Day 2/100 programs 🔹 Majority Element Problem | Java Implemented an efficient solution to the Majority Element problem in Java, where the task is to find the element that appears more than ⌊ n/2 ⌋ times in an array. 💡 Approach Used: Boyer–Moore Voting Algorithm Optimized for O(n) time complexity and O(1) space complexity 🧠 Key Learnings: Importance of algorithmic thinking over brute force How voting-based logic eliminates unnecessary comparisons Writing clean and readable Java code for interview-style problems 🚀 Why it matters: This problem is a classic example of turning a mathematical insight into a highly optimized solution—commonly asked in technical interviews and competitive programming. 📌 Tech Stack: Java | DSA | Algorithms
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