Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Joshua Bloch twitter today quiz: what is this >>>=  RSS feed

 
Giovanni Montano
Ranch Hand
Posts: 388
6
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static void puzzler() {
short i = -1, j = 0;
while (i != 0) {
i >>>= 1; j++; //what is that? How can the compiler recognize three > signs?
}
System.out.print(j); }
 
Junilu Lacar
Sheriff
Posts: 10929
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the unsigned right shift operator. See https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
 
Giovanni Montano
Ranch Hand
Posts: 388
6
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:That's the unsigned right shift operator. See https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

thank you Junilu
 
Campbell Ritchie
Marshal
Posts: 54886
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you think would happen and what happened?
 
Giovanni Montano
Ranch Hand
Posts: 388
6
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What did you think would happen and what happened?



after I read:

and

Joshua saying
yep! Compound assignment operators on "deficient types" (short, byte, char) do a silent narrowing cast (language flaw if you ask me)


I thought to trust him, after all he knows some java
 
Junilu Lacar
Sheriff
Posts: 10929
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remember correctly, the certification exam had questions pertinent to these kinds of operations, widening conversion to int then a silent narrowing conversion back to the smaller integer type.

It might interest you to note the following:

I'll leave it as an exercise for you to find the relevant JLS sections that explain why this happens.
 
Giovanni Montano
Ranch Hand
Posts: 388
6
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:If I remember correctly, the certification exam had questions pertinent to these kinds of operations, widening conversion to int then a silent narrowing conversion back to the smaller integer type.

It might interest you to note the following:

I'll leave it as an exercise for you to find the relevant JLS sections that explain why this happens.


I am going to taking up the gauntlet.

he short data type is a 16-bit signed two's complement integer. , used to save memory in large arrays( does this have sense in Java with new processors, and concurrency principles? even considering that if velocity matters in this case one could decide to go with C++? Yes because we are learning and shaping our minds..and having some fun)

255 is  equivalent to this binary '11111111' if you right shift  by 1 would be '0111111'

I do not know how to relate this to the equal cases that compile. but my assumption is
that is because >>> works with 32 and 64 bits, while we are speaking about a 16 bit value what short is


 
Stephan van Hulst
Saloon Keeper
Posts: 7476
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All arithmetical operators in Java produce a long if either of their operands is a long, and an int in all other cases.

That means that i = i + 1 doesn't compile if i is a short, because + produces an int and you can't assign an int to a short variable.

The compound assignment operators are different. They perform an implicit cast to the left hand type. So i += 1 is not the same as i = i + 1, but it's actually the same as i = (short) (i + 1).
 
Giovanni Montano
Ranch Hand
Posts: 388
6
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Interesting, so is kinda of short is 16 bit and int 32 and long 64?
It is beautiful and fascinating to go so deep, but I have to recognize  that we are going more and more out from strongly typed paradigm in java. In android that is where I am trying to get an expertise(job)  there is a much of kotin involved, because you want to type faster, and not need to allocate  adress the variables with such precision, and you want to code review the code really fast. Much more recommendable today to write clear code. I understand that learning is always a laudable act, but there are already an infinity of topics to cover, concept as bitwise operators, WeakReference, should not be considered essential imo for a today java coder, the reason most of the developers do not know is because is really difficult that these cases are found in real life examples. And as Saussure said about languages( and java belong to an artificial language) is that   a language has an universal potentiality to develop a system of signs. these signs depend  by the use. Does not have sense to say in dutch Ge(thou) today, because nobody uses, the same should be for the strongly typed languages imo
 
Stephan van Hulst
Saloon Keeper
Posts: 7476
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're mistaken about strongly typed languages being outdated. For instance, if you look at the JavaScript community, there have been various efforts to introduce a strong type system, such as TypeScript.

I agree that for certain purposes, not all language features should be considered essential. However, you made a thread about one of Joshua Bloch's puzzlers, and the entire point of these puzzlers is to illustrate corner cases in the language, not essential knowledge.
 
Junilu Lacar
Sheriff
Posts: 10929
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For reference, here's the relevant JLS passage http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.26.2 that Pertains to what Stephan pointed out.

The difference between languages like Dutch and a programming language like Java is that in computer languages, there's far less room for ambiguity as there is in everyday language. The interpretation of any given expression in a computer language must be deterministic, otherwise, it should not be interpreted into executable code at all. Therefore, behaviors like this still need to be defined even though they may not be used very often or at all in real-world situations. Another consideration, of course, is backwards compatibility. There's no way for the creators and maintainers of Java to know how the language has been used or abused out in the wild, so a complete and precise definition of behavior such as what we find in the JLS is essential.
 
Campbell Ritchie
Marshal
Posts: 54886
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote:. . . 255 is  equivalent to this binary '11111111' if you right shift  by 1 would be '0111111' . . .
That looks incorrect to me. 255 is equivalent to binary 0b0000_0000_0000_0000_0000_0000_1111_1111 because it is defined in 32 bits. Now a single right shift changes that to 0b0000_0000_0000_0000_0000_0000_0111_1111. If you simply write eight 1s, you are implying that you are working with a single byte, and 1111_1111 is not 255 in a byte, but −1.
I can't understand your last post. Don't write abbreviations like imo because many people don't know what they mean.
 
Giovanni Montano
Ranch Hand
Posts: 388
6
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Giovanni Montano wrote:. . . 255 is  equivalent to this binary '11111111' if you right shift  by 1 would be '0111111' . . .
That looks incorrect to me. 255 is equivalent to binary 0b0000_0000_0000_0000_0000_0000_1111_1111 because it is defined in 32 bits. Now a single right shift changes that to 0b0000_0000_0000_0000_0000_0000_0111_1111. If you simply write eight 1s, you are implying that you are working with a single byte, and 1111_1111 is not 255 in a byte, but −1.
I can't understand your last post. Don't write abbreviations like imo because many people don't know what they mean.

Stephan, Junilu Ritchie, your comments are really stimulating

@Stephan
Great, you are right, the OP subject is over a quiz, and has an outlier definition otherwise won't be a Joshua quiz, but maybe a more trivial certification question. Please see later anyway for an example in Kotlin that is really cool regarding strongly typed consideations.

@Junilu
Yes, backward interpretation.Bby the time a language goes in the market and the future perusal there is a gap, after all this is also a reason why you find Deprecated methods in the APIs. As natural languages. the coding artificial languages are constantly evolving.  And definetely you are right in saying that they are much more deterministic than natural ones. And this is the main point about strongly typed languages. As you have generalizations in the normal language, so you have variables declared without specifying directly the primitive  data type in a language as Java, in fact you can use the verb doing for almost everything in the natural languages, but the brain of the receiver( I mean [url=https://en.wikipedia.org/wiki/Jakobson%27s_functions_of_language]Jackobson_receiver_model[/url], in this case in Jave the brain receiver is the compiler  that  have to add meanings ( autobox, casting etc) in the unconsious/ compilation time to give a meaning to the words.
@Ritchie
Ok thanks, but jsut out of curiosity I do not see why you are speaking about 32 bits:
short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).

are you speaking of the architecture of the machine that is interpreting the 16 bits short?

My last post is over the tradeoff between optimizing the performance and making the code pragmatically adherent to the business world.
In other words market wants software delivered fast( less lines, less characters), a shared platform that allows to different members of a team to understand the code written really fast ( and more is short more easier is to read a class of course)

To all three I would like to bring an example of Kotlin to make clear how is possible to specify if needed the primitive data type.

in Java we have


in Kotlin:

For the sake of the discussion here it is a java to kotlin SimpleCheatsheet

Java is cool but verbose, a lot of boiler plate code, we live in a CI (Continuos Integration) era, where you have to write the code twice with TDD(Test Driven Development), so whatever solution make faster to write the code , faster to read it, and faster to test is welcome over the performance for the day to day tasks.

My point is, if you need for example to do HFT( High Frequency Trading) algorhitms you are going to think diffferently( possibly in C or C++, or maybe low level java concurrency architectures), but for all the todays jobs, Java can be still the standard as certified by the Tiobe index, but just we can also  consider that there is a lot of Java-->Android->(Kotlin, RxJava, new annotations libraries) responsible for the fact that today is still the most used language

Last small consideration, I understand the lack of precision of the abbrevations, but IMO, ROFL, LOL,< >, FAQ, FYI are the base of the glorius usenet times, and have been acquired also by the common language( in relation with our speech about how languages evolve), but anyway I understand that could generate problems for all the reasons underlined by the link you sent me

Last consideration according to the latest psychological studies, more one is precise, less is usually creative. Although I have to recognize the divine and strong importance to learn to become focused and attentive to even the smallest particular, weaker typed languages allow to bring much more attention to the task at hand in my opinion, but is questionable because with some robust and creative code templates one can avoid a lot of verbosity with the old and nice Java
 
Stephan van Hulst
Saloon Keeper
Posts: 7476
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm always skeptical when people cry "it's verbose!" or "boilerplate!". It doesn't actually cost a lot of time or thinking power to type words. The most time consuming part of software development is design and mental problem solving. The time it takes to type or read the extra words does not compare to that, especially when you're familiar with the language, and when you work with an IDE that has templates, macros and autocomplete.

In fact, I often find verbose languages easy to read and understand because I recognize what I'm looking at. Languages that are very dense require me to go over every character to make sure I'm not missing an important detail. I think in the long run it's actually less time consuming to develop software in verbose languages, because it's harder to introduce bugs in them.

Kotlin is strongly and statically typed. The fact that you don't have to explicitly declare the types of variables when you declare them with an initial value is because Kotlin uses type inference. It can tell what the type of the variable is supposed to be because it infers it from the value you assign to it. I admit I haven't worked with Kotlin before, but I'm pretty certain it won't compile if you try to reassign the variable with an incompatible value. Type inference avoids verbosity, but it also makes the program harder to read when you instantiate a variable using a method call that people are unfamiliar with. For instance, what is the type of the variable address in the following snippet of code?
Is it an Address? A HouseAddress? An EmailAddress? A String? You have to look at the method declaration to find out. I think type inference should only be used when you initialize a variable using a constructor or a literal, and even then it's probably useless for literals, because is it really that much more difficult to write int than var?

Regarding integer precision, if you don't care you should just use int and not worry about it. When a developer has a good reason to use short, they should probably document that. Usually it is because of interop with older software, or for high volume transactions. Languages that do type juggling (weakly typed languages) to accommodate arbitrary precision integers make it easier for the programmer to introduce subtle bugs, and may cause unexpected performance hits, because memory alignment optimizations can no longer be performed. Any programmer that doesn't understand the size of the variables they're using in their language has no business being in software development.
 
Stephan van Hulst
Saloon Keeper
Posts: 7476
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote:weaker typed languages [...] verbosity

You're confusing weak type systems with dynamic type systems. In a weak type system, variables don't have a definite type, and you can perform implicit type conversions between two values. Languages that do this are usually designed for beginning programmers who want to get stuff done quickly without thinking too much about it. It often leads to messy and buggy code.

Dynamic type systems determine the type of a value at runtime. The compiler doesn't perform type checking, and you will only find out you've performed an illegal operation when the program crashes. Dynamically typed code can actually lead to a lot less boilerplate and more readable code in some circumstances, so something can be said for its use, but in general I think it's better to have the compiler do the checking for you. I actually like the approach that C# has taken here, in that it has a static type system, but allows dynamically typed variables by declaring them with the dynamic keyword.
 
Campbell Ritchie
Marshal
Posts: 54886
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote:. . .
Ok thanks, but jsut out of curiosity I do not see why you are speaking about 32 bits:. . . .
Mainly because you didn't say anything about shorts in that post. You actually posted eight bits, which looks like a byte.
My last post is over the tradeoff between optimizing the performance and making the code pragmatically adherent to the business world.
In other words market wants software delivered fast( less lines, less characters) . . .
But the business users never see the code. They are only interested in whether it works.
. . . the lack of precision of the abbrevations, but IMO, ROFL, LOL,< >, FAQ, FYI . . . .
You are in an environment where many people didn't grow up speaking English and such abbreviations will make your posts harder for them to understand. Something like IMO has 44 different meanings here, so it will continue to cause confusion.
 
Giovanni Montano
Ranch Hand
Posts: 388
6
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: IMO has 44 different meanings here, so it will continue to cause confusion.

Unbelievable. My favorite is Itthankudi Manikanda Odhuvaar on the second page, although I cannot find a meaning even in google, but sounds good

Stephen, thank you very much to be so precise, I learned a lot in this post indeed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!