r/javahelp • • Sep 01 '19

why does "int i = 1000; byte b = (byte) i; short s = (short) i; System.out.println(b + " " + s);" give me "-24 1000"

22 Upvotes

Hello,

I just started learning java a couple of weeks ago and I'm currently doing "data changes" (not sure what it's called in English, I'm learning through a Swedish book)

So the code looks like this:

public static void main(String[] args) {

int i = 1000;

byte b = (byte) i;

short s = (short) i;

System.out.println(b + " " + s);

The way I personally translate this code is that I put the number 1000 in i, then turn b into i and s into i as well. Since both s and b are now the number that I put in i, then I was 100% that I would print "1000 1000" but instead I get "-24 1000". Where did the number -24 come from?

Also in the same exercise I wrote the code:

double d1 = 2.5;

double d2 = 2.5E40;

double d3 = 1.23456789012;

float f1 = (float) d1;

float f2 = (float) d2;

float f3 = (float) d3;

System.out.println(f1 + " " + f2 + " " + f3);

Which apparently turns into 2.5 Infinity 1.2345679. Where the did the letters infinity come from? I didn't use char anywhere.. Should the code read "2.5 2.5E40 1.2345679"?

Thankful for any guidance


r/javahelp • • Jun 17 '19

Memory leak on some systems

22 Upvotes

Hi, taking a break tearing my hair out, hoping for some suggestions.

The particulars:

JavaFX application running as a bundled exe on Java 1.8 (release 212).

Running on mix of windows 10/7. All 64bit.

The issue:

Frequent out of memory crashes on older windows 7's.

This is not a case of higher memory available preventing the crash on newer systems, as monitoring usage through visuamvm indicates that no leak occurs on newer windows 7's, or any windows 10's.

For example, starting the program on a newer windows 10 (or 7) shows a running usage of about 50 mb. Performing an action like clicking a button that loads in a new pane will raise it slightly, and spam clicking the button can spike the usage to 250-400, but as soon as a gc is performed the usage will return to the expected 50.

On older windows 7 (Various Dell Vostros from 2013) the GC's are still performed, but the overhead increases each time - after 20-30 "cycles" of loading in new panes and closing, memory usage is in the GB's and never decreases, even if a manual GC is performed, which tells me resources are not being released.

Windows 7 systems are all updated to the same level, all are running the same java versions -- so I am not sure why this leak only affects some systems. (All are similar performing intel i3's, thought the affected systems have less memory. 6gb vs 8.).

Looking at the heap dumps the items that fail to release are all in com.sun.prism.* -- which I think is tied to the javafx UI, but don't know how to diagnose and fix something like this.

Edit: feel I should clarify when this issue appeared. As code samples will only do so much in an app with 60,000 lines where most basic 'click the button' type functions result in the issue. This did not occur until 'downgrading' to Java 1.8.xx, previously Java 10. The reason for the downgrade was 'administrative insistence' on Pedro DV's excellent JMetro UI theme. The theme itself is not responsible, as the issue occurs identically with or without the theme, it's more the broader move to 1.8 vs 10+. (Also occurs with all 1.8 revisons tested, from 92-212.)


r/javahelp • • May 08 '19

Good at math to learn java

22 Upvotes

I have discalculia im horrible at math do you need to be good at math to learn coding java and java script


r/javahelp • • Apr 28 '19

Public void vs Public static void vs Public 'return Type'

22 Upvotes

Hi all

I am learning Java and am now going through some exercices (abstraction and call funtions) and was wondering the following:

As I have seen so far, Public void & Public static void & Public 'return Type' are the most used methods in Java besides the Public static void main method. Is this correct?

And secondly the difference between the three is to me:

Public void : Used when you don't have to create an object and have no return.

Public static void: Used when you need to create an object in the class itself .

Public 'return Type' (Public int, Public String, Public double): This is used when you need to return something.

Am I correct my thinking as I would like to start to do exercices in which I use an extra method in my code as most developers mention that his is better than writing it all in one class/method....

Thanks for the help.


r/javahelp • • Nov 21 '18

I want to program a simple (2D) game, but...

22 Upvotes

I wanted to try and create a simple game like snake in java, but LWJGL seems a little hard to master, where do I start (for example to make small 2D games)?


r/javahelp • • Nov 06 '17

I think I found piece of code which is 8 times slower on java 9 than on java 8

22 Upvotes

Is it my mistake or is it huge regression in java 9?

JMH Java 8: http://text-share.com/view/raw/0d59b7fd

JMH Java 9: http://text-share.com/view/raw/4020d8ad

source code: http://text-share.com/view/raw/c755b3d5


r/javahelp • • Jul 28 '17

Open Source Projects with Good Architecture, Coding Practices, and Documentation

22 Upvotes

While I like my job and the projects I am working on, day-to-day development doesn't emphasize architecture design, clean coding style, etc. To make sure I'm keeping up with best practices and aware of good design/architecture principles, I was thinking about picking up a well maintained open source probject to contribute to that has clear documentation and good feedback/code reviews from commiters when you submit a pull request. Any suggestions? For starters I was thinking maybe an apache project?


r/javahelp • • Dec 02 '15

[META] Daily coding challenges for the Advent

22 Upvotes

For those who haven't yet seen it on other subreddits or on Facebook:

Advent of Code

Has daily coding challenges during the Advent season up to the 25th of December.

Each daily challenge is split in two parts of which the second one is only unlocked after successful completion of the first part.

The challenges are relatively small and fun to make.

Have fun coding!


EDIT

In order to prevent spoilers (that would reduce the fun for the others), please do not post code directly here in the thread, post the code on pastebin, gist, github, bitbucket, or the code hoster of your choice and link here only.


Edit 2: Post stickied for the duration of the Advent of Code to make it easier to find.


r/javahelp • • Apr 25 '26

Java learning curve steep

21 Upvotes

Why the heck there is so much to learn in java i mean java basic, exception handling, collections framework, multi threading , JDBC , servlets only then i can turn to spring and spring boot...can somebody tell me if i can skip any of these topics.....i keep forgetting previous concepts 😭😭😭... it's so tough...help me 😭😭


r/javahelp • • Feb 03 '25

Can a new developer still expect to have a full career working on Java in 2025?

21 Upvotes

I am starting a new job working at a bank, and they use Java/Maven/Springboot for everything.

I am knee-deep in research and beginner courses on youtube/MOOC.fi.

I just want to know if I put my all into learning everything I can, should I be able to guarantee myself a full (35 years) career using these technologies?

I have only ever worked with C, Python, PHP, JS, Typescript, React and React Native so far in a professional setting.

I am willing to put in the work and go deep into learning everything I can, but at this point I don't know if I have the willingness to keep doing these deep-dives in so many different technologies.

Can Java be the last stop for my learning journey? I am tired of feeling like a jack of all trades, master of none.


r/javahelp • • May 09 '24

What’s the use of lambda expressions?

21 Upvotes

So what’s the main reason lambda expressions are used? So instead of developers creating the implementation for an abstract method, now we just send the implementation itself.

What’s the main point of this? It basically does the same thing? What’s the added benefit that I’m not really seeing here?


r/javahelp • • Apr 05 '24

Using final with every variable.

21 Upvotes

Hi, Does it really makes sense to use final keyword with every field, and even setters and constructors parameters?


r/javahelp • • Nov 26 '22

Solved Which list creation style is preferred?

21 Upvotes
List<String> list = new ArrayList<>(); // 1: let the compiler figure it out
List<String> list = new ArrayList<String>(); // 2: Specify the generic twice

Also, is there a technical term for when the compiler "figures it out"?


r/javahelp • • Feb 17 '22

Codeless Become a java PRO

21 Upvotes

I am a computer science student. I have my fair bit of hours on java researching and coding. I am pretty confident in my knowledge of java but it might all be ignorence. In fact, i may not have fully learnt any language in my life. I might have serious knowledge gaps. Thats my problem.

What should a java pro know? Obsiously i use data structures. I have made jar files. I know how to serialize objects. I know how to make a server and a client app. I know how to handle files. I know some basics of creating a user interface with swing.

I am not worried about my coding skills on subjects i already have experience on. I am worried about things that i dont even know exist. Could someone enlighten me with their experience? What should i know before i can confidently say that i can actually get payed for doing stuff, and not worry that i might not be able to handle it?


r/javahelp • • Jan 24 '22

Codeless Is it worth rewriting large Java project into Kotlin?

21 Upvotes

I am working on a web app with backend in Java. The project information are:

  • ~2000 java files

  • almost 200k lines of code (excluding empty lines)

  • multimodule maven project and few microservices

  • currently using Java 11, Spring Boot, Hibernate

It was decided that current code should migrate to Kotlin over time, I am trying to get an opinion on this, but from what I learned so far I think it's not worth the effort to go through migration of this scale. The project has a lot of code and design issues itself, developers (4 total) don't have a lot of experience with Kotlin, some none at all.

My questions are:

  • Do you think the migration effort would be worth it?

  • What would be the biggest advantage of having whole code base in Kotlin?

  • What would be the biggest disadvantage of the migration?


r/javahelp • • Dec 30 '21

What pattern do I need to implement for multiple jobs that call different APIs?

21 Upvotes

I am not the best at java, but I have a placeOrderJob, and based on params being passed through, be it "Grubhub", "Doordash", "UberEats", "Postmates", etc.

I need to run that particular job, since the API and logic differs between all of them.

I could write a large switch statement to differentiate, but was wondering if there was a more elegant Java pattern that would handle this.

Thanks

edit: I think what I am trying to implement is the Command Pattern? Am I on the right path?


r/javahelp • • Sep 12 '21

Unsolved Don't understand the difference between ++x and x++

21 Upvotes

So from my textbook I learned that these are used to increment variables.

However my textbook gives an example of an expression with them:

int x =10, y =20, z = ++x * y--;

It says "The third assignment increments x to 11 and decrements y to 19, but in computing z, it

uses the new value of x * the old value of y

How is it using the old value of y if it ALREADY decremented it to 19?

Aside from that,

if y-- evaluates to the old value of y, why does it return y-1 when used in a line by itself? Is returning a value different than evaluating the value?


r/javahelp • • Jun 02 '21

When would you have a Class that extends an Abstract class that implements an Interface?

21 Upvotes

Interface -> AbstractClass -> Class


r/javahelp • • Apr 19 '21

Need advice on how to debug more efficiently

21 Upvotes

Hi I'm a QA and often when I find a bug I get the stack trace and try to find exactly where is the issue and are there other places that this bug impacts and I didn't find them when I test manually.

The issue is that the system is very large and it cant be tested well if i run it all on my machine so the test env runs on the cloud.

Most if the time i get the log to see there is the error and start to imagine in my head from where the data passed how was transformed what was the input and output. If the bug is obvious and easy its not issue for me but when it something hard i cant do it. I have extensive knowledge of all the databases that we use and the interaction with the app but i want to better myself so when i report a bug to be clear when how and what happened. I'm not a fan of the "Click here then error " type of bug reporting

So is there any method that i can apply when i try to find the issue only reading the code with no debugger?

Thanks


r/javahelp • • Mar 02 '21

Codeless Best resource to learn Java 14 ?

21 Upvotes

Like I have not any experience with java before. However I have intermediate knowledge of javascript and python. My new job needs me to work on java 14. So what is the best approach to start learning that ? Sorry if the question sounds stupid.


r/javahelp • • Feb 05 '21

Jave Beginner here, still learning the basics. Need help with loops.

21 Upvotes

Hello friends,

First time poster and very new to Java. Was hoping I could get some pointers for a program I have to write for school. I believe this might be very basic for the experts here but I'm gonna take any help I can get.

So basically I have to use a formula to calculate VO2Max with the results including an age range to determine a suitable sport(s) for an athlete using data from the table below. It gets a little complicated because the age and VO2Max range overlaps one another.

SportAgeVO2Max
Basketball18 to 3040 to 60
Cycling18 to 2662 to 74
Canoeing18 to 2655 to 67
Gymnastics18 to 2252 to 58
Swimming10 to 2550 to 70
import java.util.Scanner;

public class asQ3example 
{
      public static void main(String[] args) 
      {
      Scanner keyboard = new Scanner(System.in);
      int age;
      char gender;
      double HRrest, HRmax, VO2Max = 0;
      String sport ="";
      
      System.out.print("Gender [M/F]: ");
      gender = keyboard.next().charAt(0);
     
      System.out.print("Age: ");
      age = keyboard.nextInt();
      while (age <=10 && age>=30 )  //#1
      
      {
          System.out.print("Invalid Age. Enter Age Again: ");
          age = keyboard.nextInt();
      }
      
      
      System.out.print("HRrest: ");
      HRrest = keyboard.nextDouble();
      
              
      HRmax = 220 - age;
      VO2Max = 15*(HRmax/HRrest);
      
      if (gender == 'M' || gender == 'm')
      {
          if (age >=18 && age <=30 && VO2Max >=40 && VO2Max<=60) //#2 
           {sport = "Basketball";}    
          else if (age >=10 && age <=18 && VO2Max >=50 && VO2Max<=70)
           {sport = "Swimming";}  
          else if (age >=18 && age <=25 && VO2Max>=50 && VO2Max<=51)
          {sport = "Basketball and Swimming";} 
          else if (age >=18 && age <=22 && VO2Max>=52 && VO2Max<=54)
          {sport = "Basketball, Swimming and Gymnastics";}
          else if (age >=22 && age <=25 && VO2Max>=52 && VO2Max<55)
          {sport = "Basketball and Swimming";}
          else if (age >=18 && age <=22 && VO2Max>=55 && VO2Max<=61)
          {sport = "Basketball, Swimming, Gymnastics and Canoeing";}
          else if (age >=23 && age <=26 && VO2Max>=55 && VO2Max<=61)
          {sport = "Basketball, Swimming, and Canoeing";}
          else if (age >=18 && age <=26 && VO2Max>=62 && VO2Max<=74)
          {sport = "Swimming, Canoeing and Bicycling";}
      }
      else if (gender == 'F' || gender == 'f')
      {
        if (age >18 && age <=30 && VO2Max >=43 && VO2Max<=60)
           {sport = "Basketball";}
          else if (age >18 && age <=26 && VO2Max >=47 && VO2Max<=57)
          {sport = "Bicycling";}
          else if (age >18 && age <=26 && VO2Max >=47 && VO2Max<=677)
          {sport = "Canoeing";}
          else if (age >18 && age <=22 && VO2Max >=36 && VO2Max<=50)
          {sport = "Gymnastics";}
           else if (age >10 && age <=25 && VO2Max >=40 && VO2Max<=60)
          {sport = "Swimming";}
          
      }
      else
      { 
          System.out.println("Invalid Gender");
      }
      System.out.printf("HRmax = %.1f \n", HRmax);
      System.out.printf("VO2Max = %.1f \n", VO2Max);
      System.out.printf("Recommended Sport(s) = %s \n", sport);
      }     
}

This is my code so far. You can ignore the female part as I can work on it by myself once the male part is done. There are 2 major problems I'm seeing/facing.

#1 - Setting an age range. If number falls out of range, user to be presented with error message and prompted to enter age again.

The problem I'm facing is here is that when i put the range <=10 && >=30, the script still accepts the age outside of those ranges. No error message given

#2 - the script seems to stop at the first 2 sports only even though VO2max levels and age are suitable for more than 1 sport.

The overlapping ranges are really screwing me here. I did some minor adjustments to get it to show the other sports as well but even then data could be inaccurate.

So I really need some pointers because I can't count the amount of times I feel like my head is gonna explode. The code above is in line to what we have been taught to use to answer the question.

Appreciate any kind strangers helping a beginner out!

Thank you.

PS. If there is something that I'm missing or supposed to do, please do not hesitate to let me know! Thanks


r/javahelp • • Jan 06 '21

Algorithm in Java Spring

21 Upvotes

I am Java self-taught, I just learned algorithms and spring framework. I want to master web programming using Java. I am still a student, I have never participated in real projects so I have many questions I want to be answered. I would be more than happy to receive an answer from someone.

-In Java Spring practical projects, algorithms such as recursion, backtracking, BFS, DFS, binary tree search, etc are used and used a lot? When Spring Data JPA has a search function, are search algorithms such as binary tree search, BFS, DFS, etc important and used?

- Between the algorithm and the framework, which one should I learn more deeply?

Hope someone will answer these of my problems. Thanks very much


r/javahelp • • Nov 20 '20

Homework How to swap two elements in ArrayList<ArrayList<String>>

21 Upvotes

So I have an ArrayList of an ArrayList. The first ArrayList has the lines of a txt file, and the second -the individual words from each line.

ArrayList<String> lines=new ArrayList<>();
 lines=fileIntoArray(fileName); ArrayList<ArrayList<String>>
 words=new ArrayList<>(); words.add(lines); 

I want to swap two words, word1 from line1 with word2 from line2

-Before swap

word1 word2 word3

word4 word5 word6

-After swap

word5 word2 word3

word4 word1 word6

I tried to swap them using a String temp, but it gave me an IndexOutOfBoundsException

The indexes are entered by the user

String swap1=words.get(firstLineIndex).get(firstWordIndex); 
String swap2=words.get(secondLineIndex).get(secondLineIndex); 
String temp=null; 
temp=swap1; 
swap1=swap2; 
swap2=temp;

Any help would be greatly appreciated. Thank you in advance.


r/javahelp • • Nov 08 '20

As someone who is new and willing to start coding, should i use java 8 or java 11?

21 Upvotes

I just started learning about Java and wanted to apply what I've learned. But it confused me on which version I should be using. I googled some questions, but some say to use java 8 because a lot are using it while some sayto use java 11 because people are migrating. Which one is better?