We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
Analysis Summary
Worth Noting
Positive elements
- This video provides a technically accurate and detailed breakdown of Java's semantic analysis and memory management rules using concrete code examples.
Influence Dimensions
How are these scored?About this analysis
Knowing about these techniques makes them visible, not powerless. The ones that work best on you are the ones that match beliefs you already hold.
This analysis is a tool for your own thinking — what you do with it is up to you.
Related content covering similar topics.
Java classpath tutorial (ClassLoader, NoClassDefFoundError, JVM, javac, javap -c, java -cp, jar cvf)
Fred Overflow
Unboxing Java 26 for Developers - Inside Java Newscast #108
Java
Java's Plans for 2026 - Inside Java Newscast #104
Java
Transcript
can your brain compile Java this is a fun exercise I give you some Java code and you have to predict if it compiles what the output is and if it doesn't compile why so just the slides no IDE no syntax highlighting Etc and it's um it's a lot of fun so compiling Java maybe let's see how a Java compiler works so your brain can adapt so the source code that you write is usually uh a couple of lines write a two-dimensional structure on the screen uh but the compiler just sees a very long text a sequence of characters and then you have some line breaks here indicating where the next line starts but the compiler just sees a long list of characters and in the first phase we call it the lexer converts this sequence of characters into a sequence of lexim or tokens the uh they're not the same they're not synonyms but the distinction is not important for this video okay so we have some keywords here identifiers uh keyword identifiers semicolon brace uh equal sign Etc so no problems here and then the paa tries to convert the uh sequence of tokens into a syntax tree and again there's no problem because uh the compiler understands ah we want to define a class the class has two um members uh separated with the semicolon or terminat with a semicolon okay all is syntactically valid we understand we have a class named fo two members here's the first me member is the second member names types access privilege Etc um so no problem here passing passing the the token sequence and then another phase semantic analysis comes and kicks in and then finds a problem right it determines uh wait a minute here we have a field named Fu and then we try to Define another Field named fo that's not allowed in Java right it's a semantic error um and it's not a syntax error that's the point I want to emphasize it's not a syntax error to have two variables with the same name it's a semantic error and I want to emphasize this because I promise that none of my Cent examples have syntax errors in them so you don't you don't have to chase braces or see if I maybe uh exchanged a semicolon with a colon just to just for fun or something I didn't do this so everything I show you if it doesn't compile it's always a semantic error like a type error or uh maybe um there's a method that has a return type but I forget the return statement another example of a of a semantic error right no syntax errors I promise so let's look at the first example some uh Java boilerplate code and then here is the interesting part what does xus z mean in Java and here are some possible answers maybe um if you see this X and you subtract something that isn't here maybe it stays X right okay or maybe um this is like um a sequence going from X to Z that would be x y z or maybe um the distance between x and z would be two Two Steps from X to Z or maybe the number of characters x y z is three characters uh or maybe it doesn't compile CTE stands for compile time error maybe it crashes crashes at runtime RTE stands for runtime error or maybe it's uh none of these choices right this is none of the above although the options are to the left maybe I should have written none of the left but that's not a very common um abbreviation none of the above means anything else any other answer you may come up with okay so now I I I I have no way of knowing how long you need to think about this problem so I suggest you now pause the video to think about the answer that you believe to be true and then you unpause the video and then I will explain uh what's going on okay so I hope you didn't cheat I hope you really thought about it yourself so x minus Z what what does this mean in Java so in the Java type system characters cannot be subtracted from one another but there is an implicit conversion from Char to in so uh we simply take the Unicode values of x and z which are shown here and then we perform integer subtraction and uh I hope you're convinced that if you uh subtract uh these two numbers then you get minus 2 because the second number is bigger okay so minus two is the correct answer so um lesson to learn is if uh you have chars and you perform arithmetic you always get an INT back okay so on to the next puzzle here I'm playing a bit with uh special Unicode characters uh I think they're called combining characters so this uh this special incantation back SLU 030 0 means um I want to have a French accent uh not in my voice but as a as a character and this is one of the three famous French accents so it's either either um Aon Aon gra or a clex and I I promise it's it's really one of those it's not some weird other character or something and uh well which uh which one uh does it Choose Or maybe it doesn't choose anyone at all it's also a possible answer so again pause the video and think about the answer okay so the solution depends on where you execute this code this was really confusing to me so in the eclipse console you get uh this accent uh axon graph and in the Linux terminal I didn't see any accent this was very surprising to me I would have expected the same result so um I tried jshell so if you write uh back sl030 a in jshell I think the problem becomes a little more apparent so here you can hopefully see the accent is above the quote Mark not above the a so these combining characters have to go after the character that you want to combine them with not before to me this was somewhat counterintuitive so if you say a and then the magic incantation you get the correct result and uh what's maybe a little surprising if you put this into a string Builder and then um reverse whoops reverse reverse it and then convert back to string uh we have the the arrow again because reversing on a string Builder reverses the Char values right so the a goes here and this combining character goes here and then we have the original problem again so we have you have to be very careful uh some characters in Unicode um are larger than one Char this has a historical reasons I I think I think in 1995 uh Unicode only went up to 16 bits and then it had to be extended what a mess but I'm not a Unicode expert okay beware of those Unicode issues maybe another video but not by me okay next puzzle um I'm comp comparing string the string literal fuba to certain other Expressions so so first I compareed to to well the same uh uh string literal or another string literal with the same character sequence uh then I concatenate Fu and bar with a plus sign and see if Fu bar comes out and then I concatenate Fu and bar with a concat method which does the same thing essentially as the plus okay so again pause the video and think about the answer okay so I hope you understood that um these eight combinations are all that are possible Right each um comparison could be true or false here are the eight combinations in case you w weren't aware of that and were confused pause the video again so um right so the result is true true false so if you picked um where is true true false here if you picked this one you will correct because um this operator Compares string references not string contents but the Java language specification says that if you have um two string literals with the same character sequence inside then they are guaranteed to be represented by the same string object so there's a string pool that is maintained at compile time which guarantees uh this property or maybe it has to have runtime support oh you can look this up for yourself I'm not sure and um the Java language specification also says if you concatenate string literals with plus the compiler is smart enough to do this concatenation at compil ter and again you get um the same result the the same string object but if you use the conquet method or any other method they will be executed at runtime right I suggest that you don't learn these special rules but you instead you should always use the equals method not this um not this uh operator then you won't get any surprises okay next puzzle so here we have a class with a field Setter for the field and and um a method with a parameter and all we do is write to the terminal uh the field concatenated with the parameter here we call the setter here we call the right method and well again what does a print some possible solutions cool not cool does not compile uh crash at run time or none of the above pause the program and pause the video and think about the answer okay so let's look at the answer the answer is a compile time error so we are inside the main method main is a static method meaning we call it directly on the class prefix writer we don't need any objects but of course these instance methods need an object to be called on right so we haven't created created an object yet so beginners often uh think well um I'll just make this method static and this method also and this field as well that's not the point of the exercise uh just just create an object of prefix writer like this here I've created the object uh then I call the methods on this object and the rest is basically the same so again uh pause the video and think about which of these Solutions is the the correct one okay so let's look at the answer the answer is null cool this is very interesting so here we declare a field string is a reference type so we initialize prefix with null or Java does this null is the default value for string variables because string is a reference type um and this Setter declares a local variable um of the same type and with the same name so this variable um Shadows this variable so uh this assignment or this initialization is it really um does not change the field it only changes this local variable which immediately goes out of scope so most idees should warn you hey uh you declare a variable and and you initialize it but then you never do anything with it that's probably not right um okay your ID should probably give you helpful warning here so why doesn't it uh crash with with a n pointer exception at this point um well there's a special rule in Java which says if you concatenate the null reference it is first converted to a reference to the string n l just some special Java rule so if you ever had the string null um uh on the console instead of a null pointer exception it was probably due to this Special Rule okay so on to the the next uh uh C snippet here we have a loop which goes through some uh even numbers incrementing by two each time and the question is how many dots does this program print right I just run the main method once uh from top to bottom um so how how many dots are printed here are some possible answers from from 0 to four all The Usual Suspects you see here so pause the video and try to think about the answer okay so let's look at the uh answer I hope you noticed the semicolon this is not a syntax error this is allowed in Java you can write a semicolon directly after a loop header and this means um the loop body is empty right so how however many times the the loop body is entered nothing is done please repeat nothing end times okay and after the loop is done executing we execute this block of code once because it has no association with the loop it just uh is executed after the loop okay so it's not it's it's not a syntax error to put statements in a block um just for fun right you can put blocks where a statement is required in Java no problem whatsoever and um at least my IDE doesn't warn about this so this should be a red flag and combined with this block should be a blood red flag in my opinion okay um just in case you wonder it doesn't really matter for this outcome but how how often is the empty Loop body entered or executed so we start with i equal 4 and then we compare does 4 equal 8 no it doesn't so we immediately Stop the Loop we don't even enter uh the loop body once because the condition is uh is a while condition right while this condition is true execute the loop body it is not an until condition this is uh I see beginners making this mistake all the time they write a condition they expect I to have at the end uh of the iteration okay right but doesn't change the outcome it doesn't matter how often you execute uh nothing before you execute this block once okay so next puzzle puzzle about floating Point numbers one of my uh favorite topics so we have an accumulator of type float then we have a loop inside the loop we increment the uh float with a prefix increment and well how often does this Loop um how often does this Loop body and it's really the loop body how often does it execute so we start with with zero and we pick all numbers that are at least as big as zero and we increment I so basically how many numbers are there starting from zero going up to the biggest integer okay and then what um what value does f have when the loop exits okay so uh this is integer max value so this means exponentiation on this slide it doesn't mean bitwise exclusive all integer max value this is integer max value plus one uh this means uh the loop never stops infinite Loop basically and the usual suspect so stop the video and think about the answer okay so what's the answer the answer is 16 m777 no 777 what a mouthful 216 uh in case you're unfamiliar with this notation this means 10 the^ of 7 so basically 1.6 whatever * 10 to the power of 7 which is 16 million uh whatever so why is this the case let me go into the jshell again so um 16 million 777 216 as a floting point number f means float um float literal and then I add one I hope you can see this maybe it's more obvious if I cast it to int uh it's the same number again right so um 6,777 217 is not a member of the float type okay um that's because due to the limited um number of precision bits in float uh maybe you can research this on your own very interesting topic so basically after the uh 16 millionth iteration F will never change again it will stay at this um number so um if you subtract one this is no problem right 15 just like you expect but um this number with the with a seven at the end is basically the first um it's the smallest positive integer that cannot be represented as a float so you can add two no problem so we at a range of float where only even numbers can be represented and if you add three again it doesn't exist 219 so you may expect this to be 218 again but in fact it's 220 so sometimes we round down sometime times we round up there are good reasons for that but those are not the point of this video so maybe again be aware of floating point and if you need a counter why are you using float if you want to count 0 1 2 3 Etc just use an integer that's what they're for right or a maybe a long would be better in this case due to overflow maybe you don't want to get a negative number here or something you would probably get integer Min value um otherwise okay so on to the last puzzle again strings we all love strings um so here we construct uh a Futurama character I hope you're not old enough uh I hope you are old enough to remember future Rama and then we try to extract a substring of Bender okay so uh what will this print uh I hope you're familiar with the substring operations what what the parameter means in case you're not entirely certain I I hope you you find your suspected answer in this uh long list of possible answers I hope I covered all the grounds so think about it and pause the video okay so what happens here I hope you remember that strings are immutable in Java right so um s. conat dur will indeed um have Bender as a result but uh s will will not be affected by this let me show you this in the in the jshell so I have Ben okay and then I call the con conet operate I cannot type to i conet dur and the result of this expression is Bender right here it is Bender in all his Glory but s is still bound to the string uh Ben right we we didn't change S if you want to change S you have to write s equals s do conad dur right I want to refer to it to the result of this expression and you can see it here but if if we want to check just to be sure s is now bound to the string Bender so unless you see an assignment somewhere a string variable will never change and the string objects Can't Change by definition strings are immutable in Java so if this is somewhat confusing to to you let's pick a simpler example let's have an INT variable initialized with 42 and what is a * 10 okay a * 10 is 420 I think this also has some significance can't remember right now what 420 means okay um so the result of a * 10 is 420 but a still has the value 42 right if you if you want to change a to have a different value we have to say a equals a * 42 and then um uh oh 10 it should have been 10 I'm sorry let me do it again 10 uh no sorry 42 a = a * 10 right then we have 420 I was a bit confused at this point but then I saw my mistake right okay so strings basically try to behave like numbers in Java except that you can't compare them with uh this um comparison operator as we already saw okay so um at this point in time s still has the value Ben as you can see here Ben has valid indices 0 1 and two and three is the first index after Ben this is very important and for is an invalid Index right I can't say I want a string starting here and ending here this this is an exclusive index so I could say 1 comma 3 then I will get e n and then it would stop here this is a stopping point basically an exclusive index and if I wanted to have the whole string I would say zero comma 3 then I would get b n and uh this may be confusing to you why is this not the index of the last character um it's just convenient because if you subtract these numbers you get the length of the resulting substring and if you have multiple substrings um in sequence uh you can use the same number uh uh twice let me show you this this is maybe a little confusing so if you say hello world substring uh 0 comma 5 you get hello and if you say like this I can now take the five and put it here and then um five is here 5 6 7 8 9 10 right so I can just wherever I uh stopped I can start off again and I won't miss any characters and I won't have any duplicate character so I like this convention it's um C++ has a similar convention in the STL the end iterator always points after the end not at the end okay so here are the lessons that we learned um you can't perform arithmetic operations on types below int if you have a Char it's converted to in first same for bite and short I didn't show you this um chars are not characters but Cod units very interesting to dive into unic Cod I'm not an expert uh you should use equals to compare strings not the not this operator then local variables can Shadow Fields uh in other programming languages local variables can even um Shadow other local variables if you have nested Scopes that's not possible in Java then beginners have to be careful not to put semicolons after the loop headers they get an empty Loop body um as a surprise um in case you ever need to noes this is the smallest positive in that does not F into a float so if you need integer numbers use int not float and remember that strings are immutable you can't change the the objects the string objects and if you want to refer to a different string object you have to assign to the string variable right otherwise you'll have no effect okay that's all I've got for you today if you enjoyed this I can record more uh more videos of this kind goodbye
Video description
Read some Java code and predict the output/compiler errors for fun and profit!