Java Tutorial
// declares two integer variables a and b. int a, b; // declares three integer variables num1, num2, and num3, also num1 and num3 are initialized with 15 and 25. int num1 = 15, num2, num3 = 25; // declares double variable pi with initialized value 3.14. double pi = 3.14; // declares character variable with initialized char value 'J'. char ch = 'J';
// Dynamic initialization in java class DynamicInitializationTest { public static void main(String args[]) { double a = 6.0, b = 5.0; // double variable c is here dynamically initialized // based on below expression double c = ((a * a) + (b * b)); System.out.println("c = ((a*a) +(b*b)): " + c); } }Output:
c = ((a*a) +(b*b)): 61.0
// Scope and lifetime of variables in java class ScopeTest { public static void main(String args[]) { //Visible or accessible this double variables within main method double a = 6.0, b = 5.0; double d = 0.0d; if (a > 5.0) { // double variable c is visible to only if block double c = 7.0; // variables a and b are visible in if block since inner block for method main. System.out.println("a and b: " + a + " " + b); d = a + b + c; } // c is not visible here, gets the java compiler error //c = 4.0; System.out.println("d = (a + b + c): " + d); } }Output:
a and b: 6.0 5.0 d = (a + b + c): 18.0
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page