In java, strings are immutable but not interned, so if(""==n) might or might not be true for another string for which "".equals(n) is true.
Just to confuse you more, this is bad code, it will get a NullPointerException if called with null as the argument. It should be written as "".equals(n)
Answer from ddyer on Stack OverflowCoding in Java
string - A line of java code and what it does? - Stack Overflow
I made a dumb first Java program
Bulk Refactoring of Java Code
Videos
Can anyone walk me through how to open a simple java file that I can get some Java practice in? I am a first year in uni with experience writing c programs in Dev c++ and python files in Python but I recently switched to vscode and to get into Java but I can't find a way to make a simple java file to code in. Like something that's not a whole Java project, just like one file to write my code in from scratch for practice and maybe a way to create another package to get practice with modifiers. And I want to be able to run the code, see output, ask for input etc. Is there a way for me to do this and if not could someone maybe explain this project thing to me as someone who only has experience with c files or .py files?
In java, strings are immutable but not interned, so if(""==n) might or might not be true for another string for which "".equals(n) is true.
Just to confuse you more, this is bad code, it will get a NullPointerException if called with null as the argument. It should be written as "".equals(n)
if(!n.equals(""))
{
name = n;
}
means if n is not an empty String, assign its value to name.
In Java, every Object has an equals(Object o) method to test for equality with another Object. The == operator is typically used to compare primitives. It can also be used to compare Objects for "sameness". ie. the two Objects are in fact the same instance. This comes in handy for immutable types such as Strings and all the Object wrappers for the primitive types such as Integer and Long.
Hey everyone! I was gonna start university in a few months. I do feel like I'm pretty behind but, well I'm slowly learning (stuck in tutorial hell rn). I did know some python beforehand but my university requires Java now.
I am kinda finding it great, I mean Python is simpler, but Java just gives me more of a badass vibe, idk why.
I wanted to make one these button clickers, where you click a button and there's a rising chance that you're gonna hit and bomb every time you click the button. There are a lot of ways to code it, but I used this random and count approach.
It's a little hard to understand, basically a generate a random number and check if its greater than a count that rises from 0. There would be greater probability that the generated random number will be greater than that counter as time goes on. I made a few tweaks where the program runs by itself without having to, let's say, press enter. Then it repeats this a 100 times and calculates the average score and highest score too.
To summarize it- it's pretty whack. If anyone has any suggestions, it would be great if you'd leave them below!
import java.util.Scanner;
import java.util.Random;
public class Main {
public static void main(String[] args) {
int[] counts = new int[100];
Random random = new Random();
for(int i=0; i<counts.length; i++) {
int count = 0;
int bound = random.nextInt(0,101);
while(bound>count) {
count++;
bound = random.nextInt(0,101);
System.out.println(count);
}
counts[i] = count;
System.out.println("You hit a bomb.");
System.out.println("Final score: " + count);
}
int highest = -1;
int total = 0;
for(int j=0; j<counts.length; j++) {
if (counts[j]>highest) {
highest = counts[j];
}
total = total + counts[j];
}
float average = (float) total /100;
System.out.println("\nAverage Score: " + average);
System.out.println("Highest Score: " + highest);
}
}