• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is the precedence of the 'new' and '.' operators?

 
Ranch Foreman
Posts: 626
3
  • Number of slices to send:
    Optional 'thank-you' note:
From Boyarsky-Selikoff OCP17, (tb864585.JaSE17SG.c05.10)



I thought this would be a compiler error but looking at "TABLE 2.1
Order of operator precedence
" I do not find either 'new' nor the '.'



 
Saloon Keeper
Posts: 15621
366
  • Number of slices to send:
    Optional 'thank-you' note:
In general, prefix operators have higher precedence than postfix operators.

I don't think I've ever seen a complete table of operators, regardless of the source. Even the Java tutorials don't list all available operators.
 
Marshal
Posts: 8880
638
  • 2
  • Number of slices to send:
    Optional 'thank-you' note:
To be honest, using common sense here should just work. Meaning, new instance of a class gets created, and then method gets invoked.
Not sure how that translates to precedence definition, but I don't think I'd find it very useful even if I were sure.


Prints:

Which is what I would expect.

I saw a lot of questions about the precedences in the past, but I never myself found a need to consult operators precedence table, I find it actually confusing.

i.e. let's say we have this code (traditional question of this kind):

Now, Oracle's documentation here says, that postfix (expr++) and unary (++expr) have highest precedence among all, while multiplicative (*) and additive (+) have lower precedence that those earlier.

So now one can think to no end what that means looking to that expression shown earlier.

For me, all those precedences do not matter much (and I don't know them well), but I still can calculate easily what the result will be.

It is as simple as that, evaluating expression from left to right.

We start with:
a = 1
b = 1
a + b + ++a * b++

1. Initial value of a is 1, so we place its current value to expression and so we have:

2. b is 1, so we place its current value to expression and so we have:

3. ++a means increment its current value and then place it to expression and so we have:

4. b++ means place its current value to expression and then increment and so we have:

5. Now we have basic mathematics, multiplication first, then addition:
1 + 1 + 2 which turns to 2 + 2 and then to 4, because evaluating left to right

Now, I don't know, maybe that indeed is what those precedences imply from technical language standpoint, but I just simply never think about them and use some other means (which I just showed) to figure out
 
Anil Philip
Ranch Foreman
Posts: 626
3
  • Number of slices to send:
    Optional 'thank-you' note:

Liutauras Vilda wrote: To be honest, using common sense here should just work. Meaning, new instance of a class gets created, and then method gets invoked.



Thank you for your reply. I disagree with the "common sense" part.
Common sense to the person may not be Common sense to the compiler or specification.

I expected



to be the correct way since neither 'new' nor '.' are in the precedence table.
Furthermore some operators are R to L while others are L to R.
 
Marshal
Posts: 28262
95
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:I expected


to be the correct way.



And it is. That's just common sense.

(The specification is required to go into all of that detail so that compiler writers know exactly what to do.  Like Liutauras I don't exactly know what operators take precedence over what operators, except for what you learn in elementary school math. If I'm unsure then I insert parentheses so that the correct answer is produced. And it's very rare that I write code to do arithmetic.)
 
Bartender
Posts: 5475
212
  • Number of slices to send:
    Optional 'thank-you' note:
And, even without a precedence-table, new (Rope().swing()) wouldn't make much sense. But we can have a static innerclass, then the dot takes precedence:
 
Master Rancher
Posts: 4913
74
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
I agree with Anil that "common sense" doesn't mean the same thing to different people, and as such it isn't that useful in explanations, unfortunately.  

Trying to forget what I already know from years of Java programming...I sympathize that when I look at

the spacing alone encourages me to see it as

So yeah, there's an argument for common sense to lead you in that direction.  However, what happens then?  What would "Rope().swing()" even mean?  If Rope() is a constructor (which it is), then there is no legal Java syntax for invoking that constructor without "new".  That's just the way Java was conceived - they chose to highlight object creation with the new keyword, and require it when using constructors.  Other languages like Kotlin may not require this.  But we're in Java, and Rope() by itself doesn't mean anything - so that means this way of parsing the line can't work.

As for tables of precedence for operators... one issue here is that Java's definition of "operator" may be more limited that what you may expect.  Java doesn't define new and "." as operators.  Other languages like C++ may, but not Java.  So if you see a list of precedence of all Java operators, it can still leave off other things that would be useful to know about.  The most complete list I know of is actually the Java Language Specification, specifically the JVM Chapter 15 table of contents.  It's not stated directly, but the order of the sections in that chapter is actually the order of precedence, from highest to lowest. To make that more rigorous, you need to carefully look through all the grammar definitions in this section; it's a pain.  See an example in the last post in this ancient thread.  But the upshot is, the JLS section on expressions defines higher-precedence expressions before lower-precedence expressions, in order to be able to use the higher-precedence definitions within the definitions of the lower-precedence definitions.

Specifically for the question in this thread: "new" in that code is used in a class instance creation expression, section 15.9.  The "." is part of a method invocation expression, section 15.12, which is lower precedence than the "new", therefore coming later in the chapter.  After that come things like postfix expressions, unary operators, binary operators, assignment expressions, and lambda expressions, which are even lower precedence.
 
Liutauras Vilda
Marshal
Posts: 8880
638
  • Number of slices to send:
    Optional 'thank-you' note:

Mike Simmons wrote:Trying to forget what I already know from years of Java programming...I sympathize that when I look at


Well, but this one, isn't much different from:

What I meant, that by the time one takes Oracle Certified Professional Java Programmer cert, very likely they have at least some (or they could) of experience creating an instance of some class. So deduct that from such an experience is possible without trying to trick yourself much into thinking about precedences. It is of those questions where don't need to overthink in my opinion.

Those exams are by no means easy, but there are questions that require a creative stab at them rather than try to infer and apply all the rules compiler has.
 
Liutauras Vilda
Marshal
Posts: 8880
638
  • Number of slices to send:
    Optional 'thank-you' note:
Perhaps no need to say, but I'll say anyway, @OP, don't think I'm trying to diminish your common sense. No  Just trying to help to look at some of those questions from a different angle.

 
Stephan van Hulst
Saloon Keeper
Posts: 15621
366
  • Number of slices to send:
    Optional 'thank-you' note:
Note that there are languages where new (Rope().swing()) makes absolute sense. JavaScript is one such language.

Rope() would call a function that is available in the current scope. It could return an object that contains a swing() method. The call to the swing() method could then return a constructor, which you can apply the new operator to.

For people who have experience with languages where functions and constructors are first class citizens, explicitly stating the precedence of the new and . operators isn't pointless.
 
Enthuware Software Support
Posts: 4831
52
  • Number of slices to send:
    Optional 'thank-you' note:
Since new is not an operator, the question of operator precedence does not arise. There is no need for applying precedence rules here.

As soon as you hit the new keyword, you get into the "UnqualifiedClassInstantiationCreationException" production rule, which is ( as per JLS 17 section 15.9):


The ClassOrInterfaceTypeToInstantiate cannot extend beyond the dot after parenthesis.

This means, new Rope().swing() can only be interpreted as (new Rope()).swing();


 
Mike Simmons
Master Rancher
Posts: 4913
74
  • Number of slices to send:
    Optional 'thank-you' note:

Paul Anilprem wrote:Since new is not an operator, the question of operator precedence does not arise. There is no need for applying precedence rules here.


Hmmm, I'm much more in agreement with Stephan and others that the question isn't pointless.  If you're looking at grammar production rules, you don't the concept of precedence at all (it's all implicit in the structure of the rules) - but it's a useful shortcut to gain understanding.  Whether or not it's called operator precedence is an artifact of how the JLS defines "operator", which is different from some other languages like C/C++.  I brought up the point that "new" and "." aren't operators in order to explain why they aren't included in the operator precedence table - but that doesn't mean it's a bad question.  Regardless of the definition of operator, Java programmers need to understand how expressions like this are parsed.  A more complete table of precedence for operator-ish things would actually be a useful learning tool, I think.   Unfortunately, the closest I've seen is the JLS chapter 15 table of contents.  There's probably some resource out there somewhere that does it though...
 
Paul Anilprem
Enthuware Software Support
Posts: 4831
52
  • Number of slices to send:
    Optional 'thank-you' note:
It is indeed a good question.
 
Rancher
Posts: 285
14
  • Number of slices to send:
    Optional 'thank-you' note:
i have a feeling that "new Rope().swing();" would violate some coding best practices / style guide.
it irks me to even look at it.
 
Stephan van Hulst
Saloon Keeper
Posts: 15621
366
  • Number of slices to send:
    Optional 'thank-you' note:
I do it to start my Swing applications all the time:
 
S Fox
Rancher
Posts: 285
14
  • Number of slices to send:
    Optional 'thank-you' note:
ok you got me, i do that too, but i think its the only time i do it.
 
Liutauras Vilda
Marshal
Posts: 8880
638
  • Number of slices to send:
    Optional 'thank-you' note:

S Fox wrote:but i think its the only time i do it.


Maybe you, not necessarily others. Here is another common usage.
 
Stephan van Hulst
Saloon Keeper
Posts: 15621
366
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
Before the advent of Executors, this was also a common occurrence:

(It might still be common, but it's not supposed to)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |