Java Learning Note II (Java operator)

Source: Internet
Author: User

1. ' + '; plus operator

' + ' in Java in addition to the function of the wig operation, there are two characters to connect the function

public class HelloWorld {
int i=33;
int j=44;
Char c1= ' a ';
Char c2= ' B ';
public static void Main (String args[])
{
HelloWorld hw=new HelloWorld ();
int N=HW.I+HW.J;
int c=hw.c2+hw.c1;
SYSTEM.OUT.PRINTLN (n);
System.out.println (c);
}
}

When you concatenate multiple characters, you can connect with multiple addition operators, but there are drawbacks to doing so.

2. '-'; subtraction operator

With the following example, you can understand:

public class Jian {
int i=44;
int j=22;
Char c1= ' h ';
Char c2= ' a ';
public static void Main (String args[])
{
Jian Hl=new jian ();
int N=HL.I-HL.J;
int c=hl.c1-hl.c2;
SYSTEM.OUT.PRINTLN (n);
System.out.println (c);
}
}

3. '/': division operator

You can do the division of integers, or you can do the division of floating-point numbers.

public class Chufa {
float i=23;
float j=11;
public static void Main (String args[])
{
Chufa hl=new Chufa ();
float N=HL.I/HL.J;
SYSTEM.OUT.PRINTLN (n);
}
}

Floating point division, the result is 2.090909

It seems to be a rounding operation when divisible.

public class Chufa {
int i=23;
int j=11;
public static void Main (String args[])
{
Chufa hl=new Chufa ();
int N=HL.I/HL.J;
SYSTEM.OUT.PRINTLN (n);
}
}

The result is 2.

4. '% ': Seek remainder operator

public class Yu {
int i=24;
int j=5;
public static void Main (String args[])
{
Yu Hl=new yu ();
int N=HL.I%HL.J;
SYSTEM.OUT.PRINTLN (n);
}
}

Integer remainder: The result is 4

Floating-point number for remainder, the divisor is 0, the result is Nan.

public class Yu {
float i=24;
float j=0;
public static void Main (String args[])
{
Yu Hl=new yu ();
float N=HL.I%HL.J;
SYSTEM.OUT.PRINTLN (n);
}
}

Result Output Nan

When the remainder is obtained, the divisor is negative and the result is positive

public class Yu {
int i=24;
int j=-5;
public static void Main (String args[])
{
Yu Hl=new yu ();
int N=HL.I%HL.J;
SYSTEM.OUT.PRINTLN (n);
}
}

Result output 4

When the remainder is obtained, the divisor is negative and the result is negative.

Package Yu;

public class Yu {
int i=-24;
int j=5;
public static void Main (String args[])
{
Yu Hl=new yu ();
int N=HL.I%HL.J;
SYSTEM.OUT.PRINTLN (n);
}
}

Result Output-4

Divisor and dividend are negative, the equivalent of dividend is negative, the result is negative

Package Yu;

public class Yu {
int i=-24;
int j=-5;
public static void Main (String args[])
{
Yu Hl=new yu ();
int N=HL.I%HL.J;
SYSTEM.OUT.PRINTLN (n);
}
}

Output-4

Java Learning Note II (Java operator)

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.