java 声明变量类型_Java变量类型和声明变量的规则

java 声明变量类型

Variable is nothing it is just the name of memory location.
While declaring variables we must follow rules given below.

Rules for Declaring Variables in Java

1. Variable name must bound with data type. It means while
declaring a variable we must specify its data type.
Ex: int x=10;
2. Reserve word or keywords cannot be taken as a variable
name.
3. C
supports 32 character long variable names while in Java the length of variable
name can be infinite.
4. Nothing except 0-9, A-Z, a-z, _, $ can be used in variable
name.
5. Variable name must not start with numeric and special
characters are also not allowed.
Example:
intabc=7; (valid)
int _abc=8; (valid)
int abc8=9; (valid)
int 8abc=10 (invalid)
int $_abc=12; (valid)
int $_abc*=15; (invalid)
6. Variable name should be meaningful.
Example:
string i=”Raj”; 
//valid but not meaningful
string name=”Raj”; //valid and meaningful
Note: Local variables must be initialized before first use.
class demo
{
                public
static void main(String…s)
                {
                                int
x;      //must be initialized
                                System.out.println(x);
                }
}

Java Variable Types

There are three types of variables in Java that are
instance, local and static.
1. Instance Variables
Instance variables are declared in a class, but outside any
method, constructor or block.
2. Local Variables
Local variables are declared in methods, constructors or
blocks. It must be initialized before first use.
3. Static Variables
Static variables are declared with the static keyword in a class, but outside any method, constructor or block.
class A
{
                int
x=12;               //instance variable
                staticint
y=13;    //static variable
                public
static void main(String…s)
                {
                                int
z=30;               //local variable
                                System.out.println(x);
                                System.out.println(y);
                                System.out.println(z);
                }
}
Variable is nothing it is just the name of memory location.
While declaring variables we must follow rules given below.

Rules for Declaring Variables in Java

1. Variable name must bound with data type . It means while
declaring a variable we must specify its data type.
Ex: int x=10;
2. Reserve word or keywords cannot be taken as a variable
name.
3. C
supports 32 character long variable names while in Java the length of variable
name can be infinite.
4. Nothing except 0-9, A-Z, a-z, _, $ can be used in variable
name.
5. Variable name must not start with numeric and special
characters are also not allowed.
Example:
intabc=7; (valid)
int _abc=8; (valid)
int abc8=9; (valid)
int 8abc=10 (invalid)
int $_abc=12; (valid)
int $_abc*=15; (invalid)
6. Variable name should be meaningful.
Example:
string i=”Raj”; 
//valid but not meaningful
string name=”Raj”; //valid and meaningful
Note: Local variables must be initialized before first use.
class demo
{
                public
static void main(String…s)
                {
                                int
x;      //must be initialized
                                System.out.println(x);
                }
}

Java Variable Types

There are three types of variables in Java that are
instance, local and static.
1. Instance Variables
Instance variables are declared in a class, but outside any
method, constructor or block.
2. Local Variables
Local variables are declared in methods, constructors or
blocks. It must be initialized before first use.
3. Static Variables
Static variables are declared with the static keyword in a class, but outside any method, constructor or block.
class A
{
                int
x=12;               //instance variable
                staticint
y=13;    //static variable
                public
static void main(String…s)
                {
                                int
z=30;               //local variable
                                System.out.println(x);
                                System.out.println(y);
                                System.out.println(z);
                }
}

翻译自: https://www.thecrazyprogrammer.com/2014/06/java-variable-types-and-rules-for-declaring-variables.html

java 声明变量类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值