Java program code Small details, the use of variables, as well as some details of the surface questions

Source: Internet
Author: User

Package Cn.hncu;

public class Labledemo {

public static void Main (string[] args) {
Demo1 ();
Demo2 ();
}

If you want to jump out of a multilayer loop, label technology is recommended
private static void Demo2 () {
aa:for (int i=0;i<20;i++) {
for (int j=0;j<20;j++) {
for (int k=0;k<20;k++) {
if (k==3*i+5*j-8) {
System.out.println ("i=" +i+ ", j=" +j+ ", k=" +k ");
Break aa;//jumps out of the range labeled "AA" identifies
}
}
}
}
}

private static void Demo1 () {
Boolean flag = true;
Boolean boo = true;
for (int i=0;i<20;i++) {
for (int j=0;j<20;j++) {
for (int k=0;k<20;k++) {
if (k==3*i+5*j-8) {
System.out.println ("i=" +i+ ", j=" +j+ ", k=" +k ");
Flag = false;
break;//can only jump out of a layer of loops
}
}
if (Flag==false) {
Boo = false;
Break
}
}
if (Boo==false) {
Break
}
}
}

}

---------------------------------------------

Package Cn.hncu;

public class Test3 {
private int i;//instance variable, we can use it without assigning an initial value
public void aa () {
int x;//local variable, we must assign value first to use (read the value of the variable)
System.out.println (i);
SYSTEM.OUT.PRINTLN (x);
if (x>i) {
System.out.println ("Error");
// }

if (i>=0) {
x = 50;
}

SYSTEM.OUT.PRINTLN (x);//The assignment within the block is not visible here

}
public static void Main (string[] args) {
Test3 t = new Test3 ();
T.aa ();
}

}

--------------------------------------------------

Package Cn.hncu;

public class Test4 {
private static int i; Class 1-All objects shared----The first time this class is used, Java will help us initialize the value of the
private static String Name= "Java Learning";//1 class level
private int i; 2 Object Level------in each object as long as the new object, Java will help us initialize the value of the
private static int y;
static {
y=5;
}

{//Instance block
int KK = 2; 4 block level----We have to assign values manually before we can use
}

public static void Aa () {
}
public void Bb () {
int J=3;//3 Method Level----We have to assign values manually before we can use
if (j==3) {
int k=4; 4 block level----We have to assign values manually before we can use
}
System.out.println (k);//Outside the block, you cannot access declared variables inside the block (block-level variables)

System.out.println (y);//5
}

public static void Main (string[] args) {
System.out.println (i);
New Test4 () is the most common use of TEST4 this class
TEST4.AA ();//Test4 This class
If I have a Test4 subclass, when we use subclasses, we will automatically use the Test4 class.
}

}

--------------------------------------------------

Package Cn.hncu;
/*
* Implement level 1th and then 2nd;
* Each level is the Father class, again sub-class
* Level 1th will only be executed on the first use and will not be executed after use---static is initialized only once.
* Level 2nd is performed once
*/
public class Test5 {
public static void Main (string[] args) {
The first question of the face
Father Father = new Child ();
Father = new Child ();
System.out.println ("-----------------");

The second face question
expression, the data types of the expressions on both sides of the colon must be the same (if different, they are automatically aligned to high precision)
System.out.println ((Test2 () ==3) i+15:i+10.0);
}

static int i=5;
private static int test2 () {
if (i==5) {
i = i+10;
}
return I=I/5;
}


public void aa () {
System.out.println ("AAAAA");
}
public static void xy () {
AA ();//non-static members cannot be accessed in static methods
}
}

Class father{
Public Father () {
System.out.println ("Father ...");
}
static{
System.out.println ("Father2");
}
}
Class Child extends father{
Public Child () {
SYSTEM.OUT.PRINTLN ("Child ...");
}
static{
System.out.println ("Child2");
}
}

------------------------------------------------

Package Cn.hncu;

public class Test6 {
public static void ABC () {
SYSTEM.OUT.PRINTLN ("ABC ...");
}
public void aa () {
System.out.println ("AA");
}
public static void Main (string[] args) {

}
}

Class T10 extends test6{

Java does not allow static methods to be overridden by non-static methods, and vice versa
public void ABC () {

//}

public static void Aa () {

//}

}

------------------------

Java program code Small details, the use of variables, as well as some details of the surface questions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.