Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Java Programming > Java and Operators > Java and Operators example



Clound SSD Virtual Server

Java and Operators example

Java and Operators example บทความก่อนหน้านี้เราได้รู้จักกับ Operator ในภาษา Java มาคร่าว ๆ กันแล้ว และในบทความนี้จะมาลองทดสอบการใช้งาน Operator แบบง่าย ๆ เช่น การ บวก ลบ คูณ หาร การเปรียบเทียบ ค่า และการใช้งานในรูปแบบอื่น ๆ ที่น่าสนใจ

Example 1 การ บวก ลบ คูณ หาร แบบง่าย ๆ

package com.java.myapp;

public class MyClass {
	
    public static void main(String[] args) {

        // result is now 3
        int result = 1 + 2;
        System.out.println(result);

        // result is now 2
        result = result - 1;
        System.out.println(result);

        // result is now 4
        result = result * 2;
        System.out.println(result);

        // result is now 2
        result = result / 2;
        System.out.println(result);

        // result is now 10
        result = result + 8;
        // result is now 3
        result = result % 7;
        System.out.println(result);
		    	
    }
	
}

Output
3
2
4
2
3



Example 2 การรวมข้อความ หรือ Concat String

package com.java.myapp;

public class MyClass {
	
    public static void main(String[] args) {

        String firstString = "This is";
        String secondString = " a concatenated string.";
        String thirdString = firstString+secondString;
        System.out.println(thirdString);
		    	
    }
	
}

Output
This is a concatenated string.



Example 3 การ เพิ่ม ลด ค่า

package com.java.myapp;

public class MyClass {
	
    public static void main(String[] args) {

        // result is now 1
        int result = +1;
        System.out.println(result);
        // result is now 0
        result--;
        System.out.println(result);
        // result is now 1 
        result++;
        System.out.println(result);
        // result is now -1
        result = -result;
        System.out.println(result);
        boolean success = false;
        // false
        System.out.println(success);
        // true
        System.out.println(!success);
		    	
    }
	
}

Output
1
0
1
-1
false
true









Example 4 การ เพิ่ม ลด ค่า ในตัวแปร

package com.java.myapp;

public class MyClass {
	
    public static void main(String[] args) {

        int i = 3;
        i++;
        // prints 4
        System.out.println(i);
        ++i;			   
        // prints 5
        System.out.println(i);
        // prints 6
        System.out.println(++i);
        // prints 6
        System.out.println(i++);
        // prints 7
        System.out.println(i);
		    	
    }
	
}

Output
4
5
6
6
7



Example 5 การเปรียบเทียบตัวแปรด้วย if

package com.java.myapp;

public class MyClass {
	
    public static void main(String[] args) {

        int value1 = 1;
        int value2 = 2;
        if(value1 == value2)
            System.out.println("value1 == value2");
        if(value1 != value2)
            System.out.println("value1 != value2");
        if(value1 > value2)
            System.out.println("value1 > value2");
        if(value1 < value2)
            System.out.println("value1 < value2");
        if(value1 <= value2)
            System.out.println("value1 <= value2");
		    	
    }
	
}

Output
value1 != value2
value1 < value2
value1 <= value2



Example 6 การใช้ if และ && , ||

package com.java.myapp;

public class MyClass {
	
    public static void main(String[] args) {

        int value1 = 1;
        int value2 = 2;
        if((value1 == 1) && (value2 == 2))
            System.out.println("value1 is 1 AND value2 is 2");
        if((value1 == 1) || (value2 == 1))
            System.out.println("value1 is 1 OR value2 is 1");
		    	
    }
	
}

Output
value1 is 1 AND value2 is 2
value1 is 1 OR value2 is 1



Example 7 การใช้และเปรียบเทียบค่า Boolean

package com.java.myapp;

public class MyClass {
	
    public static void main(String[] args) {

        int value1 = 1;
        int value2 = 2;
        int result;
        boolean someCondition = true;
        result = someCondition ? value1 : value2;

        System.out.println(result);
		    	
    }
	
}

Output
1



Example 8 การใช้งานในรูปแบบอื่น ๆ

package com.java.myapp;

class Parent {}
class Child extends Parent implements MyInterface {}
interface MyInterface {}

public class MyClass {
	
    public static void main(String[] args) {

        Parent obj1 = new Parent();
        Parent obj2 = new Child();

        System.out.println("obj1 instanceof Parent: "
            + (obj1 instanceof Parent));
        System.out.println("obj1 instanceof Child: "
            + (obj1 instanceof Child));
        System.out.println("obj1 instanceof MyInterface: "
            + (obj1 instanceof MyInterface));
        System.out.println("obj2 instanceof Parent: "
            + (obj2 instanceof Parent));
        System.out.println("obj2 instanceof Child: "
            + (obj2 instanceof Child));
        System.out.println("obj2 instanceof MyInterface: "
            + (obj2 instanceof MyInterface));
		    	
    }
   
}

Output
obj1 instanceof Parent: true
obj1 instanceof Child: false
obj1 instanceof MyInterface: false
obj2 instanceof Parent: true
obj2 instanceof Child: true
obj2 instanceof MyInterface: true


จากตัวยอ่างนี้จะเห็นว่า Operator ของภาษา Java จะมีความคลายคลึงกับภาษาอื่น ๆ ในตระกูลของ C , C# หรือแม้กระทั่ง PHP ก็มีรูปแบบการใช้งานคล้าย ๆ กัน และเราอาจจะเคยใช้เป็นประจำอยู่แล้ว







.

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-05-26 10:39:09 / 2017-03-27 13:57:36
  Download : No files
 Sponsored Links / Related

 
Java and Operators
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่