* Array object arrays in Java and x y parameters in the main method do not need intermediate parameters for interchange

Source: Internet
Author: User

/* Array object arrays in Java and x y parameters in the main method do not need intermediate parameters for interchange
The initialization element of the Java basic data type array is 0.
In Java, array name. Length indicates the number of elements in the array.
In the main method, argS [] starts from 0 and is the string args. length after the Java class. It also saves the number of elements.

System. Out. println () when printing an object, including the object we defined ourselves, it will call the tostring method because all classes are derived from the object and print the class test description.
We recommend that all subclasses overwrite this method to describe classes.

The basic data type is used as a parameter to pass the copy.

The referenced data type is used as a parameter to pass the reference copy.

 

*/
Import java. Lang. String;

/* Class student // Reference Data Type
{
Int age;
String name;
Student (string name, int age)
{

}
}*/
Class Test
{
 
Static void change (int x, int y) // exchange value

{
X = x + y;
Y = x-y;
X = x-y;
}
Static void change (point pt) // exchange value
{
PT. x = pt. x + Pt. Y;
PT. Y = pt. x-pt.y;
PT. x = pt. x-pt.y;
}
Static void change (INT [] num) // exchange value
{
Num [0] = num [1] + num [0];
Num [1] = num [0]-num [1];
Num [0] = num [0]-num [1];

}

 
 
Public static void main (string [] ARGs)
{




/* If (ARGs. length! = 3)
{
System. Out. println ("must have three pramarater ");
System. Exit (-1 );
}
Else
{
For (int count = 0; count <args. length; count ++) // three parameters are output cyclically.
{
System. Out. println (ARGs [count]);
}
}

*/
// Int [] num = new int [10]; // an array of the basic data type in Java will be assigned to num by the first address of the array space initialized to 0. length indicates the number of elements in the array. An Attribute referenced by the Array
/*
If num = NULL, the reference relationship between num and the array object is cut off, and the memory of the array object becomes the junk memory.
In an uncertain time, it will be reclaimed by the garbage collection mechanism.



For (INT I = 0; I <10; I ++)
{
System. Out. println (Num [I]);
}

// Reference Data Type
/* Student [] students;
Students = new student [3]; // allocate three blocks of memory but no objects are allocated. That is, each element does not save the student object, so null is printed.
For (INT m = 0; m <3; m ++)
{
System. Out. println (students [m]);
}
}*/
 
Int A = 3;
Int B = 4;
Point Pt = new point (a, B); // declare a Point Object
Int [] num = new int [] {3, 4}; // declare an array
Change (A, B );
System. Out. Print ("A =" + A + "," + "B =" + B + "\ n ");
Change (Num );
System. Out. Print ("A =" + num [0] + "," + "B =" + num [1] + "\ n ");
Change (PT );
// System. Out. Print ("A =" + Pt. x + "," + "B =" + Pt. Y + "\ n ");
System. Out. Print (PT); // overload the tostring function to output the object member Value

}
 
}

 

Class Point
{
Point (int A, int B)
{
This. x =;
This. Y = B;
}
Int X;
Int y;
Public String tostring ()
{
String STR = new string ("x =" + x + "," + "Y =" + y );
Return STR;
}
 
 
}

 

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.