/r/learnjava

Photograph via snooOG

Resources for learning Java


Resources for learning Java



  • No JavaScript. Please use /r/javascript instead.
  • No Android. Please use /r/androiddev instead.
  • No MineCraft Please use /r/Minecraft instead.
  • No Processing Please use /r/processing instead.
  • No links to your stackoverflow questions - we are not a second opinion to stackoverflow, nor are you going to get answers here when you didn't get satisfying ones there.
  • No Rewards: You may not ask for or offer payment when giving or receiving help.
  • Do not delete your posts! Deleting is selfish and will deprive others of existing solutions. There might be other people with similar problems who could profit from the discussion in the thread.
  • Do not ask for or reply with solutions as code, nor in plain text, rather comment explanations and guides. Comments with solutions will be removed and commenters will automatically be banned for a week.
  • No PM help requests or offers. Either ask your questions here and show your code, or you're out of luck. PM help requests or offers will be removed without warning.
  • No piracy! We do neither tolerate requests for pirated material, nor do we allow advocating pirated material (even mentioning that you could download commercial products for free is forbidden) - such content will be removed without warning and the poster will automatically be permanently banned from the subreddit.
  • No resource recommendations/promotions outside of the community resources thread Please post any recommendations and promotions of resources such as courses, websites and videos in the bi-weekly community resource thread.

  • Code posting
  • No screenshots of code!
  • Do not submit executable jar or compressed (zip, rar, 7z, etc.) files!
  • For small bits of code (less than 50 lines in total, single classes only), the default code formatter is fine (one blank line, then 4 spaces before each line).
  • Redditlint is a quick and simple code formatter for reddit code. Copy your code into Redditlint, click Format + Copy, and paste the code in your post (remember to leave an empty line above the code!).
  • Pastebin for programs that consist of a single class only
  • Gist for multi-class programs, or programs that require additional files
  • Github or Bitbucket repositories are also perfectly fine as are other dedicated source code hosting sites.
  • Codiva.io or Ideone for executable code snippets that use only the console
  • Repl.it - online IDE for many different programming languages
  • Google Drive, Dropbox, Mediafire, etc. are not suitable for code posting!

Free Tutorials

  • Derek Banas' Java Playlist
  • Marco Behler's youTube channel
  • Hyperskill is a fairly new resource from Jetbrains (the maker of IntelliJ)
  • Dev.java - Oracle's own Java learning platform

  • Where should I download Java?

    With the introduction of the new release cadence, many have asked where they should download Java, and if it is still free. To be clear, YES — Java is still free.

    If you would like to download Java for free, you can get OpenJDK builds from the following vendors, among others:

    Some vendors will be supporting releases for longer than six months. If you have any questions, please do not hesitate to ask them!


    Software downloads

    Official Resources

    Resources

    Programming ideas & Challenges

    Related Subreddits

    /r/learnjava

    163,546 Subscribers

    2

    Question about static methods

    In the MOOC, there is a static method m the class below: EDIT: I found this paragraph from the instructors on why they chose to use static:

    "We can already see that code dealing with string sanitization is needed in every method that handles a book, which makes it a good candidate for a separate helper method. The method is implemented as a class method since it doesn't handle object variables. "

    Is this a normal rule in writing methods/classes when a method doesn't touch any instance variables?

    public class Library {
        private HashMap<String, Book> directory;
    
        public Library() {
            this.directory = new HashMap<>();
        }
    
        public void addBook(Book book) {
            String name = sanitizedString(book.getName());
    
            if (this.directory.containsKey(name)) {
                System.out.println("Book is already in the library!");
            } else {
                directory.put(name, book);
            }
        }
    
        public Book getBook(String bookTitle) {
            bookTitle = sanitizedString(bookTitle);
            return this.directory.get(bookTitle);
        }
    
        public void removeBook(String bookTitle) {
            bookTitle = sanitizedString(bookTitle);
    
            if (this.directory.containsKey(bookTitle)) {
                this.directory.remove(bookTitle);
            } else {
                System.out.println("Book was not found, cannot be removed!");
            }
        }
    
        public static String sanitizedString(String string) {
            if (string == null) {
                return "";
            }
    
            string = string.toLowerCase();
            return string.trim();
        }
    }
    10 Comments
    2024/10/31
    23:41 UTC

    0

    Is it realistic to try and learn the listed topics within 4 days? And if yes , is there a course you guys would recommend to do so? As for my knowledge level, it is minimal (close to 0). I can maybe read it a little but definitely could not produce a code if asked to. Thanks for any help!

    • Print statements

    • Primitive types (boolean, int, double, char)

    • Math and concatenation

    • Assignment operators

    • Increment and decrement operators

    • Casting • If/else/else if statements

    • Comparison operators

    • Logical operators and short-circuiting

    • DeMorgan’s Laws and boolean simplification

    • While, do-while, for, and for-each loops

    • Scope

    • Static methods

    • Math methods (abs, pow, random, sqrt)

    • Escape sequences (\n, \t, \”, \\)

    • String methods (charAt, equals, indexOf, length, substring, substring)

    • Null

    • 1D arrays

    • 2D arrays

    • ArrayLists (add, add, get, indexOf, remove, set, size)

    • Wrapper Classes

    • Classes and objects

    • Fields, constructors, and instance methods

    • Encapsulation, inheritance, and polymorphism

    • Abstract Classes

    • Interfaces

    • Global variables and constants

    6 Comments
    2024/10/31
    21:22 UTC

    11

    java and docker

    Hi everyone. What books can you recommend for learning Docker?

    And one more question, I finished learning Spring and its core modules (spring jpa, spring security, spring boot...).

    The last technology I studied was Git.

    Now the question is what should I study next. Is it time to start learning microservices, or is it better to familiarize myself with Docker first and then learn microservices. I had a quick look at what a microservice is and there was a mention of Docker.

    Thanks

    16 Comments
    2024/10/31
    20:17 UTC

    2

    Java Books/Guides

    Recently started Computer Science college course and finding Java a bit tough. Are there any books or guides that I can buy as a reference to help me learn? I would appreciate any recommendations!!

    9 Comments
    2024/10/31
    19:21 UTC

    0

    I installed java JDK21

    Where do I type in code? Do I type in code in cmd?

    Where do I type regular hello world code? In an app? Why did I install jdk21 then? 😅

    Thank u so muchhhhhh <3

    9 Comments
    2024/10/31
    16:47 UTC

    1

    Saving and Resuming TicTacToe game

    Hello,

    Currrently learning java, I need to implement a feature that saves and resumes game,

    Will below be the correct way to do it?

    public void saveGameDataToFile(File file) {
    
        try {
            FileOutputStream fileStream = new FileOutputStream(file);
            ObjectOutputStream objectStream = new ObjectOutputStream(fileStream);
    
            objectStream.writeObject(gameBoard);
            objectStream.writeObject(player1);
            objectStream.writeObject(player2);
            objectStream.writeObject(win);
            objectStream.close();
            fileStream.close();
            System.out.println("Save game state successfully");
    
        } catch (IOException e) {
            System.out.println("Failed to save game state TicTacToe");
        }
    }
    
    
    public void loadGameDataFromFile(File file) throws ClassNotFoundException {
    
        try {
            FileInputStream fileStream = new FileInputStream(file);
            ObjectInputStream objectStream = new ObjectInputStream(fileStream);
    
            char[][] savedGameBoard = (char[][]) objectStream.readObject();
            int savedPlayer1 = (int) objectStream.readObject();
            int savedPlayer2 = (int) objectStream.readObject();
            boolean savedWin = (boolean) objectStream.readObject();
    
        } catch (IOException e){
            System.out.println("Caught in loadGameDataFromFile");
        }
    
    }
    3 Comments
    2024/10/31
    15:47 UTC

    3

    Net developer to Java learning with Hyperskill ??

    I've been in Net C# world, Microsoft SQL, Oracle since 2002 (Net 1.1)

    I also worked with Azure, JavaScript (Angular, React) since inception, just to give you an idea about my current level for your suggestions.

    I started looking into learning Java (zero experience) couple days ago, and it seems as the other cousin of C#.

    Reading many users' comments on reddit recommending Hyperskill, got me convinced about the idea, not sure though if it's the best path to master Java.

    Does it cover advanced topics ?

    What about "spring boot" or other technologies related to Java? (no idea what 'spring boot' is, I just see it mentioned as part of many java developer jobs)

    any better alternative ? suggestion, recommendation ???

    9 Comments
    2024/10/31
    13:13 UTC

    13

    Anyone that ever got a Java developer job as self taught?

    Please share how you got it

    20 Comments
    2024/10/31
    10:45 UTC

    12

    How to Escape Tutorial Hell and procrastination While Learning New Frameworks?

    I often get stuck in tutorial hell when trying to learn new frameworks. I spend too much time looking for the "perfect" course and end up making little to no progress. Btw I know Java, collections, and OOP and I'm strong in dsa. And now I really want to learn springboot for backend but I'm making no progress. I tried following it's documentation but there's just too much and I find it tough.

    What strategies can I use to break this cycle and effectively learn new frameworks without getting lost in endless tutorials? Also if possible can anyone tell some resources for learning springboot for backend?

    TL;DR: How can I escape tutorial hell and learn any technology effectively?. What’s the best approach?

    13 Comments
    2024/10/30
    21:30 UTC

    2

    Save game state

    I have built a tictactoe that works on console, I want to add a feature where users can save the game state to a file and also resume from where it stopped.

    How do I implement this? I have read about serialization and deserialization but not sure on how to utilize it for this usecase.

    - I know I should prompt the players if they wish to save or not then depending on that serialize

    - also know that when a paused game is reloaded, the program should ask if the players which to continue or if they want to restart a new game

    I have been asked not to use chatgpt as it will stunt my learning(I don't know why)

    https://stackoverflow.com/questions/19784628/saving-game-state#:\~:text=You%20can%20use%20ObjectOutputStream%20and,readObject%20to%20load%20game%20states.

    17 Comments
    2024/10/30
    15:35 UTC

    3

    How does Map actually fetch key-value pairs under the hood?

    I came across this behavior by accident while I was learning about Map:

        public static void mapDemo() {
            Map<Long, String> fruitMap = new HashMap<>();
    
            long id = 123;
            fruitMap.putIfAbsent(id, "apple");
            fruitMap.putIfAbsent(id, "dem apples!");
    
            System.out.println(fruitMap.get(123)); // Prints "null"
            System.out.println(fruitMap.get((long) 123)); // Prints "apple"
    
            var num = 123;
        }    

    Output:

    null
    apple

    When I try to retrieve the value stored against the key "123" - it depends what the type of the key is along with the value.

    If I retrieve it by just using a stray number "123" it retrieves the value `null`
    BUT if I type cast this same magic number to "long" type then the stored value "apple" is retrieved.

    I understand that for when a stray number of this size gets inferred, it is inferred to "int" type BUT why does it matter? (For example. variable `num` gets inferred to "int" type here)

    Please can someone give me an explanation for why does this happen?

    Why does the type of key also matter when fetching a value in Map?

    5 Comments
    2024/10/29
    22:47 UTC

    0

    Code readability tips

    I've done the whole first part of java mooc, now im learning java through a textbook. They differ in the way that they declare and initialize variables so I am here to ask

    1. What is the best way to declare variables
    // Declare AND Initialize
    String name = "John Doe";
    int age = 46;
    String job = "Plumber";
    
    // Declare THEN Initialize
    String name;
    int age;
    String job;
    
    name = "John Doe";
    age = 46;
    job = "Plumber";
    1. What are some other tips for better code readability you all have
    6 Comments
    2024/10/29
    19:43 UTC

    1

    How to get ready for ocp 21 exam?

    Hello, i am 19 and i've been working full time as a junior backend developer for about a year now and i want to get the ocp 21 certification

    What i want to ask is what is a realistic timeframe to get the certification if i put about 2-3 hours to it after work and also could you share some materials i could study with and tips you might have

    7 Comments
    2024/10/29
    18:39 UTC

    6

    Spring boot guidance

    I am going to start with spring spring boot so before I am going to start what are prerequisites for it, to learn something or can I directly start it also which ide is better to go for . I already have eclipse and intellij in my laptop so which can I use for it

    14 Comments
    2024/10/29
    10:19 UTC

    5

    Chat application written in Java

    Hey, I'm working on a project with my friends, a messenger based on WhatsApp, but which is actually a hybrid of our favorite features from many of the most popular messengers. This is my first time creating something like this and I wanted to ask - what are the most optimal tools in your opinion to create a real life chat, supporting both private and group conversations, saving all messages in a Mongo database and allowing multimedia transmission. Will the combination of WebSockets + Kafka be appropriate or should I look for something else?

    3 Comments
    2024/10/29
    10:11 UTC

    3

    Can't make a static reference to a non-static field

    Hi,

    I am compiling the following code:

    import javax.swing.*;
    public class StaticVarDemo {
       static int iVal1;
       static int next_a;
       int iVal2;
       int a;
       StaticVarDemo(int iIV1, int iIV2 ){
       StaticVarDemo.iVal1 = iIV1;
       this.iVal2 = iIV2;
       next_a = StaticVarDemo.iVal1;
    }
       public static void main(String[] args) {
       StaticVarDemo obj = new StaticVarDemo(10, 20);
          JOptionPane.showMessageDialog(null, " Static var iVal1="+StaticVarDemo.iVal1+"Non-static var iVal2="+obj.iVal2);
          a =  next_a;//Solution
          next_a++;
        }
    }

    When I am compiling the above code, I am getting the error, "can't make a static reference to a non-static field" on the second last line. Somebody please guide me. Zulfi.

    4 Comments
    2024/10/29
    04:13 UTC

    2

    Is it possible to learn jakarta ee concepts in spring?

    I'm part of the project that uses jakarta ee with an application server. Is it possible to learn jakarta ee within spring. This is minly because jakarta ee applications are harder to setup?

    I want to learn the @TransactionAttribute in jakarate ee but it seems like spring has its own.

    8 Comments
    2024/10/29
    00:53 UTC

    6

    New to java

    Hello i just learned the basics of programming in c++ , and this semester i will be strudying the oop with java so anyone have a good recommendation for java it will be better if its like building a game and the instructor explain everything from scratch, thanks if you have any advices or comment feel free to leave it 🫶

    5 Comments
    2024/10/29
    00:12 UTC

    6

    Somewhat off topic but after learning spring rest services how exactly do you list it as a skill, spring boot, spring framework, or spring mvc?

    Idk which is the proper way to list this on my resume/linkedin.

    3 Comments
    2024/10/27
    20:20 UTC

    5

    How to compare time in java

    I am making a program in which I want to schedule an action to happen in 30 minutes. To be more precise, someone will call the method and type in 30(minutes), then I want the method to change a boolean variable 30 minutes later. Thanks in advance!

    4 Comments
    2024/10/27
    09:46 UTC

    18

    is it possible learn this syllabus in a week(not in depth but the basics)

    OOPs Concepts and Java Programming: Introduction to Object-Oriented concepts, proceduraland object-oriented programming paradigm Java programming: An Overview of Java, Java Environment, Data types, Variables, constants, scope and life time of variables, operators, type conversion and casting, Accepting Input from the Keyboard, Reading Input with Java.util.Scanner Class, Displaying Output with System.out.printf(), Displaying Formatted Output with String.format(), Control Statements

    UNIT-II Arrays, Command Line Arguments, Strings-String Class Methods Classes & Objects: Creating Classes, declaring objects, Methods, parameter passing, static fieldsand methods, Constructors, and ‘this’ keyword, overloading methods and access Inheritance: Inheritance hierarchies, super and subclasses, member access rules, ‘super’ keyword, preventing inheritance: final classes and methods, the object class and its methods; Polymorphism: Dynamic binding, method overriding, abstract classes and methods;

    UNIT-III Interface: Interfaces VS Abstract classes, defining an interface, implement interfaces, accessing implementations through interface references, extending interface; Packages: Defining, creating and accessing a package, understanding CLASSPATH, importing packages. Exception Handling: Benefits of exception handling, the classification of exceptions, exception hierarchy, checked exceptions and unchecked exceptions, usage of try, catch, throw, throws and finally, rethrowing exceptions, exception specification, built in exceptions, creating own exceptionsub classes.

    UNIT-IV Multithreading: Differences between multiple processes and multiple threads, thread states, thread life cycle, creating threads, interrupting threads, thread priorities, synchronizing threads, inter thread communication. Stream based I/O (java.io) – The Stream classes-Byte streams and Character streams, Reading console Input and Writing Console Output, File class, Reading and writing Files, The Console class, Serialization

    UNIT-V GUI Programming with Swing- Introduction, MVC architecture, components, containers. Understanding Layout Managers - Flow Layout, Border Layout, Grid Layout, Card Layout, GridBag Layout. Event Handling- The Delegation event model- Events, Event sources, Event Listeners, Event classes, Handling mouse and keyboard events, Adapter classes, Inner classes, Anonymous Inner classes.

    am i cooked

    19 Comments
    2024/10/27
    08:55 UTC

    8

    Please help me solve I'm beginner to programming

    Write a java program to check whether a given input number by user is a Keith number or not ? (Not use array, string and it should be applicable for any n-digit number) For example let say-197 1+9+7=17 9+7+17=33 7+17+33=57 17+33+57=107 33+57+107=197

    Both are same it's Keith number

    4 Comments
    2024/10/27
    03:53 UTC

    25

    What are the best resources to learn java for backend?

    I already have experience with python frameworks like django and flask but would like to transition to java. What is the best way for a beginner to learn all the concepts?

    11 Comments
    2024/10/26
    16:03 UTC

    0

    mooc looking for x y coordinates lesson

    Hi for my next computer science class I need training from a mooc site regarding x and y coordinates and method overloading can you tell me where it is?

    1 Comment
    2024/10/26
    13:25 UTC

    2

    Other PaintComponents disappears when calling Repaint()

    https://gist.github.com/magn9195/1b455fb49b0a47b13f85ea401757fbc2

    I am a java noob, currently in the process of trying to make a snake game as my first project, however i can't seem to get all the paintcomponents to be displayed properly. Right now as soon as i move the snake background and apple disappears. Is there another way to update JPanel, so i can get my snake moving while background and apple is visible?

    10 Comments
    2024/10/26
    12:45 UTC

    19

    Seeking Java Coding Partner

    Hey everyone. I’m looking for a coding partner to write Java code with me on HackerRank and review each other’s work. If you’re interested in collaborating, please DM me! I’d love to connect and learn together.

    18 Comments
    2024/10/26
    12:19 UTC

    2

    Vertical shift for overlapping polygons on a 2D plane

    2 Comments
    2024/10/26
    09:50 UTC

    8

    [Help] Continuing beyond Spring Boot Introductory/Basics

    I've finished and followed along with the book "Spring Starts Here", which I enjoyed thoroughly. its my first exposure to Spring right after core java.

    The contents of this books are primarily just introductions and watered down examples, Im not sure where to go from here. In particularly the book didnt cover much about Spring Security (I want to know how to implement Auth and OAuth), and other stuff like using Hibernate as an ORM or more realistic examples that aren't simplified for the sake of teaching basics. (Secret vaults?)

    The book itself recommends other books such ad

    • Spring In Action
    • Spring Security In Action

    Both of which I've heard are hard to follow, and not that great of a resource.

    Am I ready to simply just use spring documentations and random articles from websites?

    Am I in a good position to attend Chad Darby's course? Im afraid that he simply covers everything I know already

    Whats the best step forward or any advice, Thanks!

    This is a follow up to my post last week, I decided to go with spring starts here and was happy I did, but yet again Im lost

    4 Comments
    2024/10/26
    08:06 UTC

    6

    I want to learn java as a high schooler.

    Hey guys, I am a class 11th student who is keen to learn java while also studying for my college entrance exams.

    I studied some concepts of java in class 9th and 10th but I guess they would be considered extremely basic.

    I was hoping to get recommendations for some online courses which could teach me java at a steady pace as I want to pursue being a software developer anyways.

    For context: the exam is the JEE.

    Thanks :D

    15 Comments
    2024/10/26
    07:51 UTC

    0

    Help with inheritance and references

    There are 3 classes A,A1,A2.
    A1 and A2 inherits A.
    I am using an object of A to initialise A1 and A2,and then, Doing object of A=object of A1.
    Now any change in A reflects on A1,can i make it so that changes reflects on both A1 and A2.
    Pseudo Class definition:
    *class A{int a;A(int m){a=m;}}.
    *class A1 inherits A{int b;
    *A1(A m,int n){super(m);b=n;}}.
    *class A2 inherits A{int c;
    *A1(A m,int n){super(m);c=n;}}.
    A ob=new A(1);
    A1 ob1=new A1(ob,2);
    A2 ob1=new A2(ob,3);
    ob=ob1;
    ob.a=69;
    print(ob1.a)//prints 69

    7 Comments
    2024/10/25
    09:48 UTC

    Back To Top