TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java

Declare Java variables

Overview

In this tutorial, we will learn to declare Java variables in Eclipse IDE. Two important points to remember is that:

  • Java is statically-typed language.
  • Java is case sensitive.

What is statically-typed Language?

Statically typed language expects the variables to be declared before using them. In Java we need to declare the variables before using them in the Java programs. Any undeclared variables would result in compilation error

<Undeclared_Variable> cannot be resolved to a variable

Syntax

The syntax for declaring a variable is:

dataType variable_name; //declaring variable

In the preceding syntax, dataType specifies a Jave type, such as int, char, double, etc. Variable can have any name that follows some specified naming conventions.

Declare Java Variables

For example to declare String variable we can use the following line of code.

String name;

Declare an integer variable i.

int i;

Declare a double variable called price.

double price;

Initialize Variable

We need to initialize the variable and assign a value. The most common way is to initialize the variable along with the declaration statement.

Examples

String name=”Learn java”;

int i=0;

double price=12.5;

 

Declare Java Variable

Common mistakes

We get this error when we try to run the program without initializing the variable.

The local variable name may not have been initialized

Java is case sensitive language.  So, String and string are not equal. We get error if we try to declare

string name=”Learn Java”;

Java is case sensitive

 

—

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/in/java/

Related Posts

Download Greenfoot Windows

Java /

Download & Install Greenfoot on Windows

Java /

Java Static Code Analysis

Java /

Java Testing Tools

Java /

Handle Multiple Exceptions in Java

Exceptions_In_Java

Java /

Exceptions in Java Programs

‹ History of Java Language› Conditional Statements in Java

Recent Posts

  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Print Triangle Pattern Flowchart
  • RAPTOR Editions
  • Flowgorithm Conditional Breakpoint Statement
  • Flowgorithm Read Numbers from File Example
  • Search Text File Flowchart Example
  • Flowgorithm Turtle Graphics Symbols
  • Draw Circle using Flowgorithm Turtle

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version