Java Experts Quiz (don't cheat!)

ilan

Expert
Licensed User
Longtime User
Will this Code Run (exactly as it is!!)?

B4X:
public class Main {
   public static void main(String[] args) {
       // \u000d System.out.println("Hello");
   }
}
 

stevel05

Expert
Licensed User
Longtime User
I read that recently too, I won't give the game away
 

Cableguy

Expert
Licensed User
Longtime User
I would say yes, as the "comment" gets broken by the slash
 

ilan

Expert
Licensed User
Longtime User
quiz 2:

what will you get as an output for this code (True or False)?

B4X:
int nr1 = 2000000000;
int nr2 = 2000000050;
System.out.println((float) nr1 == (float) nr2);
 

monic

Active Member
Licensed User
Longtime User
True

nr1 and nr2 are not equal but when repesented as a float they are.
 
Last edited:

keirS

Well-Known Member
Licensed User
Longtime User
Explain please why you think it will return true?
nr1 and nr2 are different numbers so why will they return true?

Because precision is lost when the cast is made to float. The least significant bits of the int are truncated. So the two values become the same float value.
 

ilan

Expert
Licensed User
Longtime User
Because precision is lost when the cast is made to float. The least significant bits of the int are truncated. So the two values become the same float value.

very good. and now the question is: if b4x (b4j & b4a) creates 100% native java apps so why do i get False in it?

B4X:
    Dim nr1 As Int = 200000000
    Dim nr2 As Int = 200000050
    
    Dim f1 As Float = nr1
    Dim f2 As Float = nr2
    
    Log(f1 = f2)
    Log(GetType(f1))

Waiting for debugger to connect...
Program started.
false
java.lang.Float

you can see in the logs that the casting of nr1 to float is done but still i get false, why?
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Did you test the float example in Java? I ran some tests in jshell 9.0.4.
B4X:
int n1 = 200000050
(float) n1 //2.00000048E8
(double) n1 //2.00000050E8
Looks like a Java float does have enough precision to avoid the accidental equality with 200000000.
 

ilan

Expert
Licensed User
Longtime User

yes of course I tested it in android studio and I get TRUE. you need to cast the integer to floats and compare them and you will see you will get TRUE even they are not the same.
 

sorex

Expert
Licensed User
Longtime User
the \u000d is interpret as a linefeed and seperates the code from the // comment?
 

ilan

Expert
Licensed User
Longtime User
the \u000d is interpret as a linefeed and seperates the code from the // comment?

yes correct, this characterset "\u000d" is the unicode for "new line" and when the compiler will run the code it will seperate the line to 2 lines and the code will be executed.

what benefits it could give us is, when you have a line of code that you would like to keep save from hackers or anyone that is looking on your code we could add that line in a big comments area somewhere inside and most people will not notice it.

something like this:


ofcourse it is not a 100% protection agains hackers but it could be a nice trick to hide some code from most people that doesnot know about this unicode
 

sorex

Expert
Licensed User
Longtime User
I don't think comments are added to the bytecode output.

So that "hacker" must have access to the source to get confused.
A decompiler would just show a call to some routine.

But indeed it can be handy to fool some noob who found your source
And for that job interview question he mentioned too.
Most programmers of my age know ascii/petscii values out of their head from the assembler days so the link was quickly made to what it would be.

I even saw some B4x weirdness passing by that's hard to understand so I guess that's the charm of a programming language

Are you wrapping libs yourself know that you're deep into java?
 

ilan

Expert
Licensed User
Longtime User
Are you wrapping libs yourself know that you're deep into java?

I have not wrap any libs until now but i am taking a java+android studio course for the last 2-3 month and i really start liking java.

I will start creating some apps on android studio for practice. But i must say that i miss a lot b4x stuff in android studio but still it is very powerful.
 

victormedranop

Well-Known Member
Licensed User
Longtime User

I have the same experience with kotlin with inteliJ

Victor
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…