💡 Why does System.out.print(); sometimes give a compile-time error? While learning Java, you might write something like this: System.out.println("A"); System.out.print(); System.out.println("X"); and get a compile-time error. Why does this happen? 🤔 ✔️ The reason: System.out.print(); prints output without moving to a new line, but this method requires an argument. When you leave it empty, the compiler doesn’t know what it should print, so it throws a compile-time error. In simple words 👉 print() must be given something to print. ✔️ How to fix it: 📍 If you want a blank line: System.out.println(); 📍 If you want to print a space: System.out.print(" "); Small mistakes like this help us understand how Java methods really work 💻✨ What other small Java mistakes helped you understand programming better? Drop them in the comments 👇 #Java #Programming #LearningJava #CodingBasics #SoftwareEngineering
Java System.out.print() Compile-Time Error Explanation
More Relevant Posts
-
Hello people 👋, In my previous post, I asked: What is the parent class of all classes in Java? The answer is "𝐎𝐛𝐣𝐞𝐜𝐭". In Java, every class directly or indirectly inherits from the Object class. Because of this, all classes automatically get methods like "toString()", "equals()", "hashCode()", and "getClass()". Even a simple class like: "class Student { }" still inherits all these methods from Object. Sometimes the most basic concepts are the foundation of everything we build in Java. Here are a few important ones I recently revisited: 🔹"toString()" – returns a readable string representation of an object. Example: Instead of printing a memory address, we can print something meaningful like Student ID and Name. 🔹"equals()" – compares two objects for logical equality. Example: Checking whether two user objects represent the same user. 🔹"hashCode()" – generates a hash value for an object. This is very important when working with collections like HashMap and HashSet. 🔹"getClass()" – returns the runtime class of an object. 🔹"clone()" – creates a copy of an object. Useful when we want a duplicate object without modifying the original one. 🔹"wait()" – makes the current thread wait until another thread notifies it. 🔹"notify()" – wakes up one waiting thread. 🔹"notifyAll()" – wakes up all threads that are waiting on that object. It’s interesting how many powerful capabilities in Java come from this single Object class. Which Object class method do you use the most in your projects? Comment below 👇 #Java #JavaDeveloper #JavaBackend #ObjectClass #Programming #techinsights #techjourney #learningbysharing #learnwithme #SoftwareDevelopment #learningcommunity #DeveloperJourney #CodingCommunity
To view or add a comment, sign in
-
Day 9/100 — Math Class & Type Casting in Java 🔢 Today I learned an interesting concept while working with numbers in Java. When we cast a decimal value to an integer, Java does not round the number—it simply truncates the decimal part. Example: (int) 9.99 = 9 ❗ (Not 10) If we actually want rounding, we should use the Math class: Math.round(9.99) → 10 This is an important detail because many beginners assume casting will round values, but it doesn’t. Understanding this helps avoid logical errors in programs. 💻 Challenge I tried today: Simulate 10 dice rolls using Java’s Math class and random number logic. Example approach: for (int i = 1; i <= 10; i++) { int dice = (int)(Math.random() * 6) + 1; System.out.println("Roll " + i + ": " + dice); } Learning small but powerful concepts every day and building strong Java fundamentals step by step 🚀 #Java #CoreJava #MathClass #TypeCasting #Programming #JavaLearning #100DaysOfCode
To view or add a comment, sign in
-
-
✨ DAY-36: 🌳 Understanding Object Cloning in Java – Made Simple! This visual perfectly represents how object cloning works in Java using a tree analogy. The big tree represents the original object, while the smaller trees symbolize the cloned objects created using the .clone() method. Just like these mini trees look identical to the original, cloned objects also copy the properties of the original object. ✨ Key Idea: Cloning allows you to create duplicate objects without manually copying each value. 🌱 Think of it like: Instead of planting a new tree from scratch, you simply grow multiple identical trees from one! 💡 Bonus Insight: Shallow Copy → Copies only references (faster, but linked) Deep Copy → Creates fully independent objects (safer) 📌 Cloning helps improve performance and reduces repetitive code in Java development. #Java #Programming #Coding #JavaDeveloper #OOP #Learning #TechConcepts #SoftwareDevelopment
To view or add a comment, sign in
-
-
✨DAY-6: 💻 Understanding Variables in Java – So Many Possibilities! 🚀 Every Java journey starts with one powerful concept — Variables. This fun meme reminds us that variables are the foundation of programming. They help us store, manage, and manipulate data efficiently. 🔹 int x = 10; → Stores whole numbers 🔹 double y = 5.5; → Stores decimal values 🔹 boolean isJavaFun = true; → Stores true/false 🔹 String name = "SpongeBob"; → Stores text 🔹 char grade = 'A'; → Stores a single character ✨ Variables are like containers — choose the right type, and your program becomes cleaner and more efficient. Before learning advanced concepts like OOP, Collections, or Spring Boot, mastering variables and data types is essential. Strong fundamentals build strong developers 💪 #Java #CoreJava #Variables #Programming #CodingJourney #JavaDeveloper #LearningEveryday #DevelopersLife
To view or add a comment, sign in
-
-
Functional Optics for Modern Java – Part 6: From Theory to Practice. In the final instalment of the series, Magnus Smith brings the concepts of functional optics into real-world Java development. The blog explores how ideas like composability and effect-aware updates can move beyond theory and be applied to complex domain models in a practical, maintainable way. A thoughtful conclusion to the series that balances functional programming principles with the realities of modern Java. Read the full blog here: https://buff.ly/tvH7t66 #ModernJava #FunctionalOptics #JavaDevelopement
To view or add a comment, sign in
-
♻️ Why should you always close Scanner in Java? Scanner is used to read input, from the console, a file, or a stream. But a lot of beginners (including me, early on) never bother closing it after use. Here's why that's a problem: ->it holds onto system resources even after your program is done with them ->for file-based Scanners, it can lock the file or cause data not to be flushed properly ->in larger programs, unclosed Scanners can quietly lead to resource leaks The fix is simple, either call: ✔️ sc.close() at the end ✔️ or use a try-with-resources block so Java closes it automatically While practicing Java basics, I realized the code worked either way… but one way was responsible, and the other wasn't. That's something no compiler warning will tell you. Writing correct code and writing clean, responsible code are two different things. Learning the difference early makes you a better developer. Learning in public, improving step by step 🤍 #Java #ResourceManagement #LearningInPublic #Programming
To view or add a comment, sign in
-
Day 14 of Programming - 🚀 Sorted Arrays in Java – Quick Guide Sorting arrays is one of the most common tasks in programming. Java provides a simple way to sort arrays using the Arrays.sort() method from the java.util package. 🔹 Example: Sorting an Integer Array import java.util.Arrays; public class SortArray { public static void main(String[] args) { int[] numbers = {5, 3, 8, 1, 2}; Arrays.sort(numbers); System.out.println(Arrays.toString(numbers)); } } ✅ Output: [1, 2, 3, 5, 8] 🔹 Key Points ✔ Arrays.sort() sorts elements in ascending order by default ✔ Works with int, double, char, String, and objects ✔ For objects, implement Comparable or Comparator 🔹 Example: Sorting a String Array String[] days = {"Saturday", "Monday", "Wednesday", "Thursday", "Tuesday"}; Arrays.sort(days); 📌 Output: [Monday, Saturday, Thursday, Tuesday, Wednesday] 💡 Tip: Sorting is often used in searching, data analysis, and algorithm optimization. #Java #Programming #Coding #JavaDeveloper #DataStructures #Arrays #TechLearning #SoftwareDevelopment
To view or add a comment, sign in
-
-
I learned a surprising Java concept. Two Java keywords exist… But we can’t actually use them. They are: • goto • const Both are reserved keywords in Java. But if you try to use them, the compiler throws an error. So why do they exist? Let’s start with goto. In older languages like C and C++, goto allowed jumping to another part of the code. Sounds powerful, right? But it often created messy and confusing programs — commonly called “spaghetti code.” When Java was designed, the creators decided to avoid this problem completely. Instead of goto, Java encourages structured control flow using: break continue return This makes programs easier to read and maintain. Now the second keyword: const. In languages like C/C++, const is used to declare variables whose value cannot change. Java handles this differently using the final keyword. Example: final int x = 10; Once declared final, the value cannot be modified. And here’s the interesting part Java kept goto and const as reserved words so developers cannot accidentally use them as identifiers. Sometimes the smartest design decision in a programming language is what it chooses NOT to include. Learning programming isn’t just about syntax. It’s about understanding why the language was designed this way. A special thanks to my mentor Syed Zabi Ulla for explaining programming concepts with such clarity and always encouraging deeper understanding rather than just memorizing syntax. #Java #Programming #Coding #LearnToCode #DeveloperJourney
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
-
-
✨DAY-17: 🌳 Understanding Strings in Java – A Real-World Example Learning Java becomes easier when we connect concepts to real life. This image explains Strings in Java using trees as an example: 🔹 Single Tree with One Rope – Just like a simple string reference. 🔹 Multiple Trees Connected by Ropes – Represents the String Pool, where identical string values share memory. 🔹 Separate Trees with Separate Ropes – Represents new String() objects, which create new memory even if the value is the same. 💡 Key Insight: In Java, string literals share memory inside the String Pool to optimize performance, while using new String() creates a new object in heap memory. Understanding this concept helps in: ✅ Writing memory-efficient code ✅ Avoiding unnecessary object creation ✅ Improving performance in large applications Sometimes, the best way to understand programming is to visualize it in nature 🌱 #Java #Programming #CodingLife #JavaDeveloper #LearningJourney #TechConcepts
To view or add a comment, sign in
-
More from this author
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
Great progress Chathurani Thuduwage! Love seeing people invest in learning Java and growing their skills. If you’re looking for structured practice, feel free to check out our free course: https://www.javapro.academy/bootcamp/the-complete-core-java-course-from-basics-to-advanced/ Keep up the awesome work!