TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Saturday, 7 December 2019

Constant in java | constants and variables

Constant in Java


A constant is a variable whose value cannot change once it has been assigned. Java doesn't have built-in support for constants.
A constant can make our program more easily read and understood by others. In addition, a constant is cached by the JVM as well as our application, so using a constant can improve performance.
To define a variable as a constant, we just need to add the keyword “final” in front of the variable declaration.

  

Syntax :-

final float pi = 3.14f;


In the above statement declares the float variable “pi” as a constant with a value of 3.14f. We cannot change the value of "pi" at any point in time in the program. Later if we try to do that by using a statement like “pi=5.25f”, Java will throw errors at compile time itself. It is not mandatory that we need to assign values of constants during initialization itself.
In the below example, we can define the primitive datatype (byte, short, int, long, float, double, boolean and char) variables as constants by just adding the keyword “final” when we declare the variable.

java data types and variable
constant in java


Example :


public class ConstantsDemo {
   public static void main(String args[]) {
      final byte var1 = 2;
      final byte var2;
      var2 = -3;

      final short var3 = 32;
      final short var4;
      var4 = -22;

      final int var5 = 100;
      final int var6;
      var6 = -112;

      final long var7 = 20000;
      final long var8;
      var8 = -11223;

      final float var9 = 21.23f;
      final float var10;
      var10 = -121.23f;

      final double var11 = 20000.3223;
      final double var12;
      var12 = -11223.222;

      final boolean var13 = true;
      final boolean var14;
      var14 = false;

      final char var15 = 'e';
      final char var16;
      var16 = 't';

     // Displaying values of all variables

      System.out.println("value of var1 : "+var1);
      System.out.println("value of var2 : "+var2);
      System.out.println("value of var3 : "+var3);
      System.out.println("value of var4 : "+var4);
      System.out.println("value of var5 : "+var5);
      System.out.println("value of var6 : "+var6);
      System.out.println("value of var7 : "+var7);
      System.out.println("value of var8 : "+var8);
      System.out.println("value of var9 : "+var9);
      System.out.println("value of var10 : "+var10);
      System.out.println("value of var11 : "+var11);
      System.out.println("value of var12 : "+var12);
      System.out.println("value of var13 : "+var13);
      System.out.println("value of var14 : "+var14);
      System.out.println("value of var15 : "+var15);
      System.out.println("value of var16 : "+var16);
   }
}
 



 

No comments:

Post a Comment