d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help Me Understand This Plz
Add Reply New Topic New Poll
Member
Posts: 31,627
Joined: Mar 25 2009
Gold: 116.00
Oct 17 2015 04:27am
Code
class ScopeDemo {
public static void main(String args[]) {
int x;
x = 10;

if (x == 10) {
int y = 20;
System.out.println("X and Y: " + x + " " + y);
x = y * 2;
}
y = 100;
System.out.println("x is " + x);
}
}



I don't get why y = 100; is invalid, yet system.println("x is " + x); gives the output x is 40....

y = 100, is inside the main method... and so is x.... it seems a bit hypocritical that Y can't be used outside the if statement, yet X is used outside the if but holds the value of inside the if statement.... i really don't get how this works
Y inside if statement is declared, yet outside the if doesn't work..............BUT x is assigned value of 40 inside the if statement, yet still has the value of 40 outside it rather then 10 when it was originally initialized
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 17 2015 06:38am
when you see braces, you should think scope as a hierarchy. anything declared in an outer brace can be used inside (like x), whereas anything declared inside a brace (like y) cannot be used outside. didn't you take c#? it's the same way there.
Member
Posts: 31,627
Joined: Mar 25 2009
Gold: 116.00
Oct 17 2015 07:29am
Quote (carteblanche @ Oct 17 2015 08:38am)
when you see braces, you should think scope as a hierarchy. anything declared in an outer brace can be used inside (like x), whereas anything declared inside a brace (like y) cannot be used outside. didn't you take c#? it's the same way there.


exactly my point

x = y * 2; ------is used inside brace (if statement), yet it's still prints x = 40, instead of x = 10...... Just like Y = 20 in if statement, yet can't use outside, YET x still = 40.... they're declared in the same if statement yet 1 works outside it and the other doesn't
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 17 2015 07:35am
Quote (ferf @ Oct 17 2015 09:29am)
exactly my point

x = y * 2; ------is used inside brace (if statement), yet it's still prints x = 40, instead of x = 10...... Just like Y = 20 in if statement, yet can't use outside, YET x still = 40.... they're declared in the same if statement yet 1 works outside it and the other doesn't


why would you expect x = 10? are you perhaps confusing the = operator? keep in mind = is the assignment operator, not an equation-equality statement like in math.

for example:
int firstNumber = 10; // firstNumber holds 10
int secondNumber = firstNumber + 1; // firstNumber holds 10, secondNumber holds 11
firstNumber = 50; // firstNumber holds 50, but secondNumber still holds 11, not 51

the equal sign = doesn't mean secondNumber will always contain a value 1 higher than firstNumber. the value firstNumber + 1 is computed at runtime and assigned to secondNumber. secondNumber will hold this value until you assign it a new one. exact same situation in your example, except the braces seem to be confusing you.

see how i specified the values in comments on a line-by-line basis? do that with your example to show me your reasoning.

This post was edited by carteblanche on Oct 17 2015 07:38am
Member
Posts: 31,627
Joined: Mar 25 2009
Gold: 116.00
Oct 17 2015 08:32am
Quote (carteblanche @ Oct 17 2015 09:35am)
why would you expect x = 10? are you perhaps confusing the = operator? keep in mind = is the assignment operator, not an equation-equality statement like in math.

for example:
int firstNumber = 10; // firstNumber holds 10
int secondNumber = firstNumber + 1; // firstNumber holds 10, secondNumber holds 11
firstNumber = 50; // firstNumber holds 50, but secondNumber still holds 11, not 51

the equal sign = doesn't mean secondNumber will always contain a value 1 higher than firstNumber. the value firstNumber + 1 is computed at runtime and assigned to secondNumber. secondNumber will hold this value until you assign it a new one. exact same situation in your example, except the braces seem to be confusing you.

see how i specified the values in comments on a line-by-line basis? do that with your example to show me your reasoning.


It's the braces as you said.. I know how it'd act without them.... But ur not getting what i'm saying i don't think.....
(more specifically what's inside the IF statement), why is y = 100 excluded and not work when x = 40 does?

This post was edited by ferf on Oct 17 2015 08:32am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 17 2015 08:46am
Quote (ferf @ Oct 17 2015 10:32am)
It's the braces as you said.. I know how it'd act without them.... But ur not getting what i'm saying i don't think.....
(more specifically what's inside the IF statement), why is y = 100 excluded and not work when x = 40 does?


refer to my previous comment.

Quote
when you see braces, you should think scope as a hierarchy. anything declared in an outer brace can be used inside (like x), whereas anything declared inside a brace (like y) cannot be used outside.


tell me, what is the scope of x and y? scope of x is the function, scope of y is the if block. you cannot use it out of scope.

if you're still confused, google for java variable scope.

This post was edited by carteblanche on Oct 17 2015 08:49am
Member
Posts: 31,627
Joined: Mar 25 2009
Gold: 116.00
Oct 17 2015 09:06am
Hmmm I think I understand it now, just seems weird logic to me....
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 17 2015 09:10am
perhaps you'll like javascript better?

http://jsfiddle.net/#&togetherjs=0FT8wnbVnP

Code
var x;
x = 10;

if (x == 10) {
var y = 20;
x = y * 2;
}
y = 100;
alert("x=" + x + ", y=" + y);


// shows x=40, y=100
Member
Posts: 31,627
Joined: Mar 25 2009
Gold: 116.00
Oct 17 2015 09:11am
Quote (carteblanche @ Oct 17 2015 11:10am)
perhaps you'll like javascript better?

http://jsfiddle.net/#&togetherjs=0FT8wnbVnP

Code
var x;
x = 10;

if (x == 10) {
var y = 20;
x = y * 2;
}
y = 100;
alert("x=" + x + ", y=" + y);


// shows x=40, y=100


nah
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll