search
HomeJavajavaTutorialCommon classes in Java

Common classes in Java

Dec 10, 2016 am 09:38 AM

API: Application Program Interface, application programming interface. In layman’s terms, it is a set of class libraries provided by the system.

Find the lang package in the API documentation, which contains many commonly used classes.

1.Basic type

Basic data type Class type

byte Byte

short Short

int Integer

long                                                                    

boolean Boolean

These eight categories Types have their own attributes and methods respectively. Let’s give an example:

Integer class

Attributes:

Maximum value of int: Integer.MAX_VALUE

Minimum value of int: Integer.MIN_VALUE

Method:

Convert an integer to a string: Integer.toString(100);

Convert an integer to a string corresponding to the base system: Integer.toString(a,2);

Convert a purely numeric string to an integer: Integer.valueOf ("12345");

Convert a pure numeric string to an integer: Integer.valueOf("1100011",2);

Convert a pure numeric string to an integer: Integer.parseInt("12345");

Convert a purely numeric string to an integer: Integer.parseInt("1100011",2);

Convert an integer to binary: Integer.toBinaryString(a);

Convert an integer to hexadecimal: Integer.toHexString(a);

Convert an integer to octal: Integer.toOctalString(a);

Example:

1. Convert the positive integer 90 to binary, octal, hexadecimal

java code uPublic class commonly used category {

Public Static Void Main (String [] ARGS) {

int a = 90;

String s = Integer.tobinaryString (a); // A Convert to binary System.out.println(s);

println(s1);

String s2=Integer.toHexString(a);//Convert a to hexadecimal System.out.println(s2);

String s3=Integer.toString(a, 2);//Convert a is output as a string in binary form.

Attribute: E

PI

Method: Math .abs(t); //The absolute value of a

Math.max(a,b); //The maximum value of a

Math.min(a,b); //The minimum value of a

Math. pow(a,b); //The b power of a

Math.sqrt(a); //The square root of a

Math.random(); //Randomly obtain a number between 0.0 and 1.0

3. System class

attributes: System.err.println();

          System.out.println();

            System.in

method: System.exit(0);                .

System.currentTimeMillis(); //Get the current system time

System.arraycopy(Object src,int srcPos,Object dest,int destPos, int length); //Copy the array

Parameter 1: Source array

parameter 2: The starting subscript position in the source array

Parameter 3: The target array

Parameter 4: The starting subscript position in the target data

制 Parameter 5: Number of copies

Example: int [] a = {1, 2, 3, 4, 5};

int [] b = new int [10]; , 1, b, 4, 3);

Java code

import java.util.Scanner;

public class Common class 1 {

public static void main(String[]args ) { Er n Scanner sc = New Scanner (System.in);

System.out.println ("Please enter the division and divide number:);

int a = sc.nextint ();

int b = sc.nextInt();

int[]array1={2,9,11,5,8,3};

int[]array2=new int[6];

if(b==0){

            System.err.println ("The dividend cannot be 0");

System.out.println(c);

long x=System.currentTimeMillis();

System.out.println("The current time is "+x);

System.arraycopy(array1, 1, array2, 2, 4);

System. out.print("The data in array array2 is ");

                                                                                                                           ;

                                                                                                                      There are several different sets of standards.

Commonly used character set standards:

ISO-8859 Western European character set, excluding full-width characters

GB2312/GBK Simplified Chinese character set

Big5 Traditional Chinese character set

UTF-8 Character set based on Unicode encoding

ANSI means using the local default character set standard

Construction method

String(byte[] bytes)

String(byte[] bytes, "Character set encoding")

String(byte[] bytes,start( Starting subscript), length (take a few))

String(char[] c)

String(char[] c,start,length)

Method:

Convert string to byte array byte bs[]=s.getBytes();//Get a string array

The string is converted into a byte array according to the specified character set byte bs[]=s.getBytes("UTF-8");

Convert the string into a character array Char c[]=s.toCharArray();

Copy some characters in the string to the character array getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);

Get the character at the specified position char c=s.charAt(subscript);

Compare two strings in dictionary order compareTo(String anotherString);

Compare two strings in dictionary order A string, regardless of case compareToIgnoreCase(String str);

Determine whether the string contains another string contains(CharSquence(parent class) s);

Determine whether the string is based on a certain ends with a suffix endWith(String s);

Determine whether the string starts with a certain prefix startsWith(String s);

Determine whether the two strings are equal equals(String s);

Determine whether two strings are equal, ignoring case equalsInnoreCase(String s);

Determine the position of the substring in the large string indexOf(s1);

Determine the position of the substring in the large string The last occurrence position in s.lastIndexOf(s1);

Get the character length of the string .length(); The length of the array .length;

will replace the string s.replace("oldChar","newChar");

will replace the first string s.replaceFrist("oldChar","newChar");

intercept characters String s.substring(startIndex(start index));

Convert the replacement string s.substring(startIndex,endIndex(end index));

to lowercase toLowerCase(s1); Uppercase toUpperCase(s1);

Remove the blank characters at the beginning and end of the string trim();

Convert other types of data into string type String.valueOf (any type);

Cut the string String[] s=s1.split();

String class I will give an example, using two methods to display Monday to Friday

Java code

public class lianxi5 {

public static void main(String[] args) {

                                                                                                                                                                                                            

                                String  z="" ;

                                                                use ’ s ’ using use using ‐         ‐     ‐ ‐                                                            

          char c=s.charAt (i); z = z+c;

}

for (int j = 0; j & lt; str.Length (); j ++) {

t = str.charat (j);

System.out.print(z+t+" ");

}

} System.out.println();

System.out.println(); for(int i=0;i

          t= str.charAt(j);

                  System.out.print(st+t+"   "); Wednesday Thursday Friday Saturday

Sunday Monday Tuesday Wednesday Thursday Friday Saturday

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Java Platform Independence: Compatibility with different OSJava Platform Independence: Compatibility with different OSMay 13, 2025 am 12:11 AM

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

What features make java still powerfulWhat features make java still powerfulMay 13, 2025 am 12:05 AM

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

Top Java Features: A Comprehensive Guide for DevelopersTop Java Features: A Comprehensive Guide for DevelopersMay 13, 2025 am 12:04 AM

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.

Is Java Truly Platform Independent? How 'Write Once, Run Anywhere' WorksIs Java Truly Platform Independent? How 'Write Once, Run Anywhere' WorksMay 13, 2025 am 12:03 AM

JavaisnotentirelyplatformindependentduetoJVMvariationsandnativecodeintegration,butitlargelyupholdsitsWORApromise.1)JavacompilestobytecoderunbytheJVM,allowingcross-platformexecution.2)However,eachplatformrequiresaspecificJVM,anddifferencesinJVMimpleme

Demystifying the JVM: Your Key to Understanding Java ExecutionDemystifying the JVM: Your Key to Understanding Java ExecutionMay 13, 2025 am 12:02 AM

TheJavaVirtualMachine(JVM)isanabstractcomputingmachinecrucialforJavaexecutionasitrunsJavabytecode,enablingthe"writeonce,runanywhere"capability.TheJVM'skeycomponentsinclude:1)ClassLoader,whichloads,links,andinitializesclasses;2)RuntimeDataAr

Is java still a good language based on new features?Is java still a good language based on new features?May 12, 2025 am 12:12 AM

Javaremainsagoodlanguageduetoitscontinuousevolutionandrobustecosystem.1)Lambdaexpressionsenhancecodereadabilityandenablefunctionalprogramming.2)Streamsallowforefficientdataprocessing,particularlywithlargedatasets.3)ThemodularsystemintroducedinJava9im

What Makes Java Great? Key Features and BenefitsWhat Makes Java Great? Key Features and BenefitsMay 12, 2025 am 12:11 AM

Javaisgreatduetoitsplatformindependence,robustOOPsupport,extensivelibraries,andstrongcommunity.1)PlatformindependenceviaJVMallowscodetorunonvariousplatforms.2)OOPfeatureslikeencapsulation,inheritance,andpolymorphismenablemodularandscalablecode.3)Rich

Top 5 Java Features: Examples and ExplanationsTop 5 Java Features: Examples and ExplanationsMay 12, 2025 am 12:09 AM

The five major features of Java are polymorphism, Lambda expressions, StreamsAPI, generics and exception handling. 1. Polymorphism allows objects of different classes to be used as objects of common base classes. 2. Lambda expressions make the code more concise, especially suitable for handling collections and streams. 3.StreamsAPI efficiently processes large data sets and supports declarative operations. 4. Generics provide type safety and reusability, and type errors are caught during compilation. 5. Exception handling helps handle errors elegantly and write reliable software.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor