In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the basic exercises of the basic grammar class of Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the basic exercises of Java basic grammar class"?
The use of functional classes
There are some commonly used classes in Java that can be used. Today we are going to learn # about mathematical classes Number class and Math class. One is to box built-in data types into objects, and the other is to perform related operations in mathematics.
Number class
Although there are basic data types in Java syntax, sometimes we need to use data as objects, so Java encapsulates several built-in data types of numbers, such as int, short, long, float, double and byte, into classes, which are Integer, Short, Long, Float, Double, Byte, and they are all subclasses of Number class, inherited from Number class.
And because these classes are modified with the final modifier, they cannot be inherited and rewritten. (as we will talk about later on in inheritance, it probably means that the parent class has, the child class has, and the child class can have what the parent class does not have.)
There are two more wrapper classes for basic data types, one is the Character class and the other is the Booleam class. The Number class is in the java.lang package, and its subclasses are also in java.lang. This wrapper, which is particularly supported by the compiler, is called boxing, so when the built-in data type is used as an object, the compiler boxing the built-in type as a wrapper class.
Similarly, the compiler can unbox an object into a built-in type.
Define the declaration to use the
There is no difference between the method and the built-in or data type: write the type name / class name + space + variable name + assignment operator + value + semicolon. Take Integer as an example:
Public class Te0 {
Public static void main (String [] args) {
Integer a = 10
B = a + 10
System.out.println (b)
}
}
one
two
three
four
five
six
seven
Running result
twenty
one
Other classes are used in the same way
Mtah class
Java's Math contains properties and methods for performing basic mathematical operations, such as elementary index, logarithm, square root and trigonometric functions. Because the Math class is a static class (static), the methods can be called directly, and the same Math class can be used directly without import.
I love math. I love math.
Basic usage
Two important attributes
Since it is a class used for mathematical operations, we have to mention two quantities, one is π\ pi π (pi 3.1415926... ), one is e (natural constant).
These two quantities are also available in the Math class, which are modified by final to represent immutable constants
π\ pi π is Math.PI in the Math of Java
E is Math.E in the Math of Java.
Public class Te1 {
Public static void main (String [] args) {
System.out.println (Math.PI+ "" + Math.E)
}
}
one
two
three
four
five
Output result:
3.141592653589793 2.718281828459045
one
Common methods
If you don't say much, we'll go straight to the practical information! Practical information?! Is it really practical information? It is strange that the author can write practical information.
Find the absolute value of abs ()
Math.abs (- 10); / / result: 10
Ceil () rounding up
Math.ceil (5.2); / / result: 6.0directly let individual position + 1
Floor () rounding down
Math.floor (5. 7); / / result: 5. 0 directly let the individual position-1
Maximum value of max ()
Math.max (5,6.2); / / result: 6.2 compares the size of two numbers and returns the larger
The b power of pow () a
Math.pow (2,3); / / result: 8.0 returns a result of 2percent, and the result is of type double.
Random () random number
Math.random (); / / result: a random number of 0-1. Note that there are no parameters in it.
Round () is rounded
Math.ronud 11.1; / / result: 11
Sqrt () to find the square root
Math.sqrt (2); / / result: 1.4142135623730951
Sin () cos () tan () asin () acos () atan () trigonometric function and inverse trigonometric function
After a simple method, you have to practice? Or take a look at the results of the operation? Yeah, here we go.
System.out.println (Math.sin (Math.PI/2)); / / result: 1.0 to find the sine value
System.out.println (Math.cos (Math.PI/2)); / / result: 6.123233995736766E-17 calculates the cosine
System.out.println (Math.tan (Math.PI/2)); / / result: 1.633123935319537E16 calculates the tangent
System.out.println (Math.asin (0.5); / / result: 0.5235987755982989 calculate the inverse function value of sin
System.out.println (Math.acos (0.5)); / / result: 1.0471975511965979 calculate the inverse function of cos
System.out.println (Math.atan (0.5)); / / result: 0.4636476090008061 to calculate the inverse function of tan
one
two
three
four
five
six
String related classes
A total of these: String, StringBuffe, StringBuilder three kinds of String can not be modified, when modifying the string, you need to use the StringBuffer and StringBuilder classes.
The biggest difference between StringBuilder and StringBuffer is that the method of StringBuilder is not thread-safe (cannot be accessed synchronously).
Because StringBuilder has a speed advantage over StringBuffer, the StringBuilder class is recommended in most cases
String class
Define
Direct definition method
String str = "Hello World!"
Object creation method
String str1 = new String ("Hello World!")
What is the difference between the two methods? First, if the string content is the same, then two variables will point to the same string, which can be understood as a person has more than one name (screen name); if the second content is the same, comparing them with the equals () method will return false, because they are two instantiated objects, which can be understood as: there are two people with the same name in the world, but not one person.
Use
String concatenation
Use + sign stitching
System.out.println ("Hello" + "" + "World" + "!"); / / result: Hello World!
Use the concat method
System.out.println ("Hello" .concat ("World!")); / / result: Hello World!
String formatting
It is through some kind of symbol to format the string output, it can be understood like this: if the string I want to use is variable, for example, when I run, I shout every lap: run x circle. Then we can use string formatting.
Some people will say, when I used it before, didn't I use the + sign?
Yes, you can use the + sign, but if there are multiple quantities, won't + seem a little cumbersome?
So after we have learned, we can use the method of formatting strings, which is not only high, but also easy to use, isn't it beautiful?
System.out.printf ("He%slo Wo%sld!", "l", "r")
/ / the result of the first method is that Hello World% s represents a placeholder and can represent any type of
/ / the same placeholder also has% d, which represents the numeric
System.out.println (String.format ("He%slo Wo%sld!", "l", "r"))
/ / the result of the second method is Hello World!
one
two
three
four
five
String comparison
System.out.println ("a" .equals ("b")); / / result: false
one
The string finds the subscript by element
System.out.println ("appdata" .indexOf ("t")); / / the result is: 5 if not found,-1 is returned
one
String length
System.out.println ("appdata" .length ())
/ / the result is: 7 returns the length of the string
one
two
StringBuilder class
Unlike String, the object of the StringBuilder class is mutable, which means that we can operate directly on the original object without generating a new object.
Define and use
StringBuilder is a class, so instantiate the object through the new keyword
StringBuilder S1 = new String ("hello world"); / / write the string directly for initialization
StringBuilder S2 = new StringBuilder (5); / / define a length without initialization
System.out.println (S1 + "" + S2)
S1.append ("12345")
S1.append ("666")
System.out.println (S1 + "" + S2)
/ *
The output is as follows:
1234512345666 12345
, /
one
two
three
four
five
six
seven
eight
nine
ten
Common methods:
String .append (); A parameter adds new content to the original string. The first four steps above are to add 12345 to the empty string.
StringBuilder S1 = new StringBuilder ("hello world")
S1.append (!)
System.out.println (S1)
/ / result: hello world!
one
two
three
four
String .insert (); two parameters
StringBuilder S1 = new StringBuilder ("hello world")
S1.insert (10 "!")
System.out.println (S1)
/ / result: hello world!
one
two
three
four
The first parameter is the string subscript, and the second is the content, which is added before the subscript
3. The string .delete () has two parameters; both parameters are subscripts, the first is the starting subscript and the second is the terminating subscript, deleting the content between the two subscripts.
StringBuilder S1 = new StringBuilder ("hello world!")
S1.delete (0,4)
System.out.println (S1)
/ / the result is: world!
one
two
three
four
StringBuffer class
Like the StringBuilder class, the StringBuffer class is a mutable object.
Define
StringBuffer S1 = new StringBuffer ("123")
/ / instantiate the object to S1 and initialize it to 123
StringBuffer S1 = new StringBuffer ()
/ / instantiate the object to S1 without initialization
one
two
three
four
Use
StringBuffer S1 = new StringBuffer ("123")
System.out.println (s1.append ("1")); / / append 1, output: 1231
System.out.println (s1.replace ("1", "2")); / / replace 1 with 2, output: 2232
System.out.println (s1.insert (3, "2")); / / insert 2 after subscript 3, output: 22322
System.out.println (s1.delete (1,2)); / delete values from subscript 1 to subscript 2, output 222,
one
two
three
four
five
That's all, but as I said at the beginning, these are all basic exercises, so that's it.
Scanner class and random class
One is the class that controls the input, and the other is the random number class.
Scanner class
Get the data from the keyboard, which is located under the java.util package. You need to import it first.
1. Upper hand
Import first
Import Java.util.Scanner
one
Re-instantiate
Scanner scan = new Scanner (System.in)
/ / instantiate a class named scan for the Scanner class to receive data obtained from the keyboard
one
two
Use
Int a = scan.nextInt ()
/ / declare the integer variable a, and store the data obtained from the keyboard in a. Input must be an integer, otherwise an error will be reported.
Float b = scan.nextFloat ()
/ / declare a single-precision floating-point variable b, and store the data obtained from the keyboard in b. The input must be a single-precision floating-point number, otherwise an error will be reported.
Char c = scan.nextChar ()
/ / declare the character variable c, and store the data obtained from the keyboard in c. The input must be of character type, otherwise an error will be reported.
String d = scan.next ()
/ / declare the string d, and store the data obtained from the keyboard in d. The input must be a string, otherwise an error will be reported.
System.out.println (a + "" + b + "" + c + "" d)
/ / you can call them through variable names as you would with ordinary variables.
one
two
three
four
five
six
seven
eight
nine
ten
All right, no, that's all I can do. I'm short of knowledge.
Random class
Random number class, simple and practical, really super simple usage
Define
Random rd = new Random ()
/ / the first method: instantiate a Random class object
Random rd2 = new Random (10)
/ / the second method: instantiate an object according to the seed value
one
two
three
four
Use
System.out.println (rd.nextInt ()); / / generate a random integer
System.out.println (rd.nextDouble ()); / / generate a 0-1 double precision floating point number
/ / … Among other things, the use of the name of the basic data type in uppercase next+ is similar to the way Scanner receives data
System.out.println (rd.nextInt (5)); / / generate a random integer between 0 and 5
System.out.println (rd.nextDouble () * 5); / / generate a decimal of 0-5
one
two
three
four
five
At this point, I believe you have a deeper understanding of "what are the basic exercises of the basic grammar class of Java?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.