i was trying to write a code for checking a Palindrome number as below | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

i was trying to write a code for checking a Palindrome number as below

public class PalindromeNumber{ static int n = 939; static int x = n; static int a; static int sum=0; public static void main(String[]args){ while(n>0){ a = n%10; sum = (sum*10) + a; n = n/10; } if(sum==x){ System.out.println(x+" is a Palindrome Number."); } else{ System.out.println(x+" is not a Palindrome Number."); } } } but here my confusion is even if i don't give the variable sum the value of 0. it works meaning int sum = 0; works and int sum; works too. i meant it works both ways, whether i give sum the value or not it works ...why..

1st Aug 2019, 4:55 AM
stephen haokip
stephen haokip - avatar
1 Answer
+ 2
Because the default value of a variable is zero when declared as a class member. Local variables, instead, need initialization.
1st Aug 2019, 6:02 AM
Davide Mornatta
Davide Mornatta - avatar