prepping for an exam, new to java & might need help understanding some stuff
im just gonna put in questions here as I study
ill probably not ask a whole ass program explanation unless its fully indecipherable to me
ill bold whatever is the tldr version if you dont want extra context
31 Replies
⌛
This post has been reserved for your question.
Hey @Piglet | NEED tone tags!!! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
firstly, whats a null literal 😭
ik it represents like no value but the text says "null is used to initialize an object of a class ... and may denote non-availability of a character in a String"
secondly, ill need to do conversions from/to decimal, binary, hexadecimal and octal numbers but if anyone happens to know a method thats easy for any of that pls lmk
this is what we've been taught, I just have some trouble w I. & II. cuz im not great at calculations
null is a special value to indicate that there is no object. https://stackoverflow.com/a/19654040/10871900
The null literal is the text
null
that refers to that valueStack Overflow
What is the difference between null, 0 and nothing?
What is the difference between null, 0 and nothing?
I cannot find a question addressing all three of these.
For example:
If I am getting input from a string and parsing it to int.
return Integer.
So every reference variable (so variables that are not primitives like boolean, char, short, int, long, float, double can be null meaning "no value associated with it"
and you can explicitly set a variable to
null
using yourVariable = null;
or check whether a variable is null using if(yourVariable == null) {}
Stack Overflow
What is null in Java?
What is null?
Is null an instance of anything?
What set does null belong to?
How is it represented in the memory?
okay so like, its ONLY for non-primitive datatypes, but if I wanted to do pretty much the same thing w a primitive data type, smth like
int example = 0
or String ex2 = ""
works rightInteger.parseInt
and Integer.toString
, both support a radix specifying the base (e.g. 2 for binary, 10 for decimal, 16 for hexadecimal)
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Integer.html#parseInt(java.lang.String,int)
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Integer.html#toString(int,int)oh I cant use code
The value
0
is a normal int and String
s are not primitive so a String
can be null
(which is usually not that good of an idea)its a written exam, might have to write some programs but other than a simple calculator I cant use anything else
okay, thanks
Well you can still use
Integer.parseInt
om your cpdeI mean the questions for that are gonna be like:
"Convert 110101001 to hexadecimal" and id have to work it out myself
oh you mean without code?
There are a few techniques for it
for program questions theyre like "A Piglatin word is the part of the word after the first vowel, the part before, added with an 'ay'" and I'd have to use like substring and loop commands to get that output
yup
You can convert a binary number to decimal like this: The leftmost bit is 1, then 2, then 4, then 8, etc - you multiply each bit with the corresponding number and add them up
For converting decimal to hex, you divide it by 16 and take the remainder. That's the last digit. You continue using the result of the division and repeat
For binary<--> hex conversion specifically, it's easier
you just split it in groups of 4
yeah
and then you convert each group to a decimal number and then that decimal number is the hex digit (ofc use ABCDEF for numbers between 10 and 16)
okay I just wanted to check if anyone knew a quicker method but thats what we've been taught so thats resolved ig
whats the
(String args[])
part of public static void main (String args[])
for?these are program arguments
like ik that its the main method class, but ive seen
(String []args)
and just now (String p)
For main arguments specifically:
You can start a program using
java YourMainClass.java
/java your.fully.qualified.Main
/java -jar yourjar.jar
.
You can also add additional text after that. So you'd start it with java YourMainClass.java your arguments here
/java your.fully.qualified.Main your arguments here
/java -jar yourjar.jar your arguments here
. In this case, args
woulöd have the String
s your
, arguments
and here
And parameters in general:
If you declare a method like this:
then you can call it with someMethod();
but if you declare a method like this:
then you can call it with someMethod("Hello World");
(or with any other String
) and you can use the passed value in the method:
see also https://dev.java/learn/classes-objects/defining-methods/ and https://dev.java/learn/classes-objects/calling-methods-constructors/
Dev.java: The Destination for Java Developers
Defining Methods - Dev.java
Adding methods to a class definition.
Dev.java: The Destination for Java Developers
Calling Methods and Constructors - Dev.java
How to pass information to a method or a constructor.
hm im getting some obsidian file linking deja vu
OHHH aliases
Why?
?
like if a note is called 'Monkeys' you can add alternative titles or aliases within the note and while linking it in another note both will work as links, like both show up as options
and idk that feels kinda similar to this thing, not too much of a similarity since the
(String somestring)
isnt exactly an alias, but since both someMethod
and someMethod("Hello World")
works its just enough to remind me of itwierd comparison probably but idk
anyw im taking a break
I know obsidian linking but I don't really see the connection
If you pass a primitive parameter, it copies the value
but objects are references to the same object and if you pass that, you get a new reference to the same object
ig that might be what you mean
idk my brain just understands shit by trying to figure out if it resembles smth ik already so that can mean the most obscure connections that even I dont really understand
That's perfectly fine as long as you don't make too many assumptions thinking it would work similarly
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.