This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Micro Frontends in Action and have Michael Geers on-line!
See this thread for details.
Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

ternary operators

 
Ranch Hand
Posts: 115
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello ranchers,i have a doubt in the code
when i compile the code with the else statement the program run and gives the output as 4,but when i remove the else statement it shows an error: Bingo.java:8: variable a might not have been initialized
System.out.println(a);
^
1 error



can someone explain me why this happens??
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is because- the variable a is never initialized. And with condition evaluating to "false" always the block right after the if doesnt get executed. So when you get that exception. When you add an else block- the variable a gets initialized. One option is to initialize the variable a when you are declaring it or add the else block.
 
sumedha rao
Ranch Hand
Posts: 115
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your quick reply,how can i do the same thing without an else block
 
Bartender
Posts: 2265
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably already replied, initialize while declaring.
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Just to add "local variable always needs to be initialized before use" and if initialization occurs on a conditional way e.g. inside an if statement then you have to provide else statement to make sure it initialize on all cases.

here if you want to run your program without else you can initialize int a=0 ;( or any default value) at the time of declaration.

Cheers
Javin
 
Rancher
Posts: 3635
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sumedha rao wrote:thanks for your quick reply,how can i do the same thing without an else block


The same thing as what? It's hard to tell what you're trying to do in the original code - it seems unnecessarily complex, checking for things that could not possibly happen. What are you trying to achieve?

In particular, why would you write "if (x)" and then immediately check again if x is true or not? And then in the else clause, check yet again to see if x is true or not? It seems like this whole thing could be written easily with EITHER (a) a single if/else, or (b) a single ternary operation. Not both. And not one if/else with TWO ternary operations. How many times do you need to test if x is true or not?
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The sad part is, the original question is about scoping.... and the ternary operators confuse the question.

Grrrrr......

Here's (a slightly sarcastic way) in response to how to do it without a ternary and without an else....



 
Mike Simmons
Rancher
Posts: 3635
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yes, you can also shorten the entire program to

But this goes back to what I asked: what is the original poster actually trying to do? It's not clear.

Also, I don't see how the original question is about scoping at all. It seems to be about the rules of definite assignment, which is making sure a variable has been assigned to be something before you try to use it. Variable a is in scope for most of the program (from the moment it's declared to the end of the main method). The problem is that it hasn't been definitely assigned - if we comment out the else clause.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic