Some java Fundamental [closed] - java

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Update::
1). problem ::
i wrote System.out.println("welcom"); &
System.out.println("india");
i want like :: welcome india
but give::
welcome
india

1.
println always prints a newline character. To get what you want, try:
System.out.print("welcome ");
System.out.print("india\n");
2.
x++ is post-increment, ++x is pre-increment.
So in:
int x = 5;
int y = ++x;
y will be 6.
But in:
int x = 5;
int y = x++;
y will be 5.

In 1: What is the difference between System.out.print and System.out.println ?
In 2: What does the ++ operator do and when does it do it?

println
stands for "print line" so, if you use println you have a "carriage return" and the next println will print a new line (and so on...)
x++ is different to ++x: the second increment variable's value and then goes on, the first goes on with statement and then increment variable's value.

System.out.println("welcom"); & System.out.println("india");
you wrote now if you want same line
System.out.print("welcom ");
System.out.print("india");
And
x++ is Post-increment
Post-increment : add 1 to the value.
The value is returned before the increment is made, e.g.
x = 1;
y = x++;
Then y will hold 1 and x will hold 2
and
++x is pre increment
Pre-increment : add 1 to the value.
The value is returned after the increment is made, e.g.
x = 1;
y = ++x;
Then y will hold 2 and x will hold 2.

1) To print a string without printing an endline use System.out.print()
2)
x++ - increment x, and return x's previous value (before increment)
++x - increment x, return x's current value (after increment)
These are the Post and Pre fix versions of incrementing.

println() prints a line and anything printed after the println statement (using any print statement) is printed in the next line.
Second thing is the difference between ++x and x++. ++x increases the value of x BEFORE doing anything else in the statement (pre-increment), and x++ increases the value of x AFTER it performs anything else in the statement (post increment).
For example:
x = 1;
y = ++x; // value of y is 2, value of x is 2
but
x = 1;
y = x++; // value of y is 1 and x is 2

try System.out.print & 1 happens before a condition/expression takes place; the other after;

System.out.println() has give next line as it complete statment & ++x are use for preincremet & C++ is post incremental.

Related

In Java,operator precedence, increment and decrement, why isn't the first println statement executed? [duplicate]

This question already has answers here:
How increment and decrement with if condition
(2 answers)
Closed 5 months ago.
class Example{
public static void main(String args[]){
int x=99;
if(x++==x){
System.out.println("x++==x : "+x); //Why this code line is not run?
}
if(++x==x ){
System.out.println("++x==x : "+x);
}
}
}
Why isn't the first println statement executed?
The operands of an expression are evaluated left to right.
In the expression x++ == x, first x++ is evaluated. It increments x by 1, but returns the original value of x. So x++ returns 99.
Then x is evaluated, which returns 100 (since it was incremented by x++).
Since 99 is not equal to 100, this condition evaluates to false.
If you change the expression to x==x++, you'll get true.
The differece between i++ and ++i is very simple.
i++ - means exactly first get the value and then increment it for the further usage
++i - means exactly first increment the value and use the incremented value
Following the shippet x++ == x means following:
Analyze expression from left to the right
Get x = 99 as the left operand and use it in the expression
Increment x and thus x == 100
Get x = 100 as the right operand (note it is already incremented)
99 != 100
Following the shippet ++x == x means following:
Analyze expression from left to the right
Get x = 99 as the left operand
Increment x and thus x == 100 and use it in the expression
Get x = 100 as the right operand (note it is already incremented)
100 == 100
You can see all these logic. E.g. not experienced developer cannot know these details. Therefore the best practice is to avoid such increments in the expression. Just do it before the expression in the single line. In this case the logic will be straight forward and you get much less problems.

Increment/Decrement Confusion

What is happening when we decrement the code here:
temp[--countArray[getDigit(position, input[tempIndex], radix)]]
If temp is 1 in this case: are we decrementing first so that we are assigning to 0? How immediate is this decrement? It always seems to confuse me within array brackets.
Try unpacking the brackets on different levels of indentation:
temp[ // get this index in temp
-- // decrement by 1
countArray[ // get this index in countArray
getDigit(position, input[tempIndex], radix) // call getDigit()
]
]
In human-readable terms, it calls getDigit() to index into countArray, then decrements that value and uses it to index into temp.
The decrement operator --x is different from x-- because of what it returns. By the end of the operation, x always ends up as one less than it was, but --x returns the new value of x, while x-- returns the old value of x from before it was decremented. The same applies for ++x and x++.
Let me break this down some. Here's some code that's equivalent to above:
int digit = getDigit(position, input[tempIndex], radix);
countArray[digit]--;
int count = countArray[digit];
temp[count] // Do something with the value
Incidentally, this is a classic illustration of why you shouldn't sacrifice clarity to brevity.

java pre increment and post increment and self-assignment in loop [duplicate]

This question already has answers here:
What is x after "x = x++"?
(18 answers)
Java increment and assignment operator [duplicate]
(6 answers)
Closed 4 years ago.
I expected post-increment and pre-increment of a variable and assignment of the result to itself to work a bit differently. But while the latter works as expected, the former runs as infinite while loop. Could someone please point out what i am missing here?
int y = 0;
int z = 4;
while(y<z)
{
System.out.println(y);
y =y++;//this prints 0 infinite times, shouldn't why be assigned values 0,1,2,3 with each pass?
//y =++y;//this works as expected
}
Thank you
As described in this StackOverflow answer, post increment works by storing a copy of y, adding 1 and returning the copy. This means that the return value of y++ is not y+1, but still only y. Since you are overwriting y with y++, you are essentially just saying y = y.

Why do people write --i? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Incrementing in C++ - When to use x++ or ++x?
I've seen things like i++ commonly used in, say, for loops. But when people use -- instead of ++, for some reason some tend to write --i as opposed to i--.
Last time I checked, they both work (at least in JavaScript). Can someone tell me if this is so in other C-family languages, and if it is, why do some people prefer --i to i--?
++i is faster than i++. Why? See the simple explication.
i++
i++ will increment the value of i, but return the pre-incremented value.
temp = i
i is incremented
temp is returned
Example:
i = 1;
j = i++;
(i is 2, j is 1)
++i
++i will increment the value of i, and then return the incremented value.
Example:
i = 1;
j = ++i;
(i is 2, j is 2)
This is simillar for --i and i--.
I don't believe the preference for prefix (++i) vs. postfix (i++) varies depending on whether it's an increment or a decrement.
As for why, I prefer prefix where possible because my native language is English, which has mostly a verb-before-object structure ("eat dinner", "drive car"), so "increment i" or "decrement i" is more natural for me than "i decrement". Of course, I'll happily use postfix if I'm using the result and I need the value prior to the increment in an expression (rather than the incremented value).
Historically the increment/decrement operator were motivated by stack management. When you push an element on the stack:
*(stackptr++) = value
when you pop:
value = *(--stackptr)
(these were converted to single ASM instructions)
So one gets used to increment-after and decrement-before.
You can see another idiomatic example in filling in direct and reverse order:
for (p=begin;p!=end;)
*(p++) = 42;
for (p=end;p!=begin;)
*(--p) = 42;
It's pretty much a preference thing, that only comes into play if you're doing the pre- or post-increment on types that are more complicated than primitives.
Pre-increment ("++i") returns the value of i after it has been incremented, and post-increment ("i++") returns the value before increment. So, if i was of a complicated type that had overloaded the pre-increment and post-increment operators, the pre-increment operator can just return the object, whereas the post-increment would have to return a copy of the object, which could well be less efficient if the object is substantial. And in most cases (for loop increments, etc.), the value is ignored anyways.
Like I said, most of the time, not a problem, and just a preference thing.
In C and C++,++ and -- operators have both prefix form: ++i and --i, and suffix form: i++ and i--. Former means evaluate-then-use and latter means use-then-evaluate. The difference is only relevant when either form is used as part of an expression. Otherwise it's a matter of preference or style (or lack thereof).
F.ex., in int i = 0; int n = ++i;, i is first incremented and then its value, now 1, is assigned to n. In n = i++ value of i, still 1, is assigned to n and is then incremented, with i == 2 now.
When used as a statement, that is: i++; ++i; both forms are equivalent.
The difference is pre-incrementing or post-incrementing (and likewise with decrementing).
Quoting the Wikipedia article on the topic that shows up as the second Google result for "c decrement":
int x;
int y;
// Increment operators
x = 1;
y = ++x; // x is now 2, y is also 2
y = x++; // x is now 3, y is 2
// Decrement operators
x = 3;
y = x--; // x is now 2, y is 3
y = --x; // x is now 1, y is also 1
So, it's not so much a matter of preference as a matter of difference of results.

postincrement operator interesting behaviour in Math.min()

I have a question,
In Java, does Math.min bind tighter than ++?
Let me illustrate with an example and maybe someone can explain to me why I get the results I get.
Here's a method I run:
private static void testIncrement() {
int x=10;
System.out.println(x++);
System.out.println(x);
x=10;
System.out.println("-----------");
System.out.println(++x);
System.out.println(x);
x=10;
System.out.println("-----------\n"+x); //10
x=Math.min(255, x++);
System.out.println(x); **//x=10 WHY NOT x=11?**
x=10;
System.out.println("-----------\n"+x);
x=Math.min(255, ++x);
System.out.println(x);
}
The results are:
10
11
-----------
11
11
-----------
10
10
-----------
10
11
On the line where I put //x=10 WHY NOT x=11?
I wonder why x is 10 and not 11. Maybe someone can explain this to me.
It looks as if Math.min create a copy of x (which is 10 at this time) which it uses to do Math.min. Then the original x is incremented from 10 to 11, but the copy which is still 10 comes out of Math.min and overwrites the incremented one.
Does this make sense?
Does anyone have an explanation for why x is 10 and not 11 in this case?
Thanks
PS - I totally understand How do the post increment (i++) and pre increment (++i) operators work in Java?
Let's deconstruct this line:
x = Math.min(255, x++);
The x++ means "remember the original value of x; then increment x; then the value of the expression is the original value". All of this happens before the assignment. So it's equivalent to:
int tmp = x; // x = 10, tmp = 10
x = x + 1; // x = 11, tmp = 10
x = Math.min(255, tmp); // x = 10
Hopefully that should make it clear. In particular, this has nothing to do with Math.min itself - it's just behaving as a normal method invocation. See section 15.14.2 of the JLS for more details.

Categories

Resources