Title: Java not equal Example - Java Code Geeks
Open Graph Title: Java not equal Example - Java Code Geeks
Description: Check out our detailed example about Java not equal! we will talk about what is != in Java in general and for what reason we use it.
Open Graph Description: Check out our detailed example about Java not equal! we will talk about what is != in Java in general and for what reason we use it.
Mail addresses
support@javacodegeeks.com
Opengraph URL: https://examples.javacodegeeks.com/java-not-equal-example/
Generator: WordPress 6.9.4
Domain: examples.javacodegeeks.com
{"@context":"https://schema.org","@graph":[{"@type":"Article","@id":"https://examples.javacodegeeks.com/java-not-equal-example/#article","isPartOf":{"@id":"https://examples.javacodegeeks.com/java-not-equal-example/"},"author":{"name":"Ioannis Makrygiannakis","@id":"https://examples.javacodegeeks.com/#/schema/person/fa6b72698bbeff000a70a501caeb524d"},"headline":"Java not equal Example","datePublished":"2020-01-17T09:00:00+00:00","dateModified":"2021-08-10T09:01:20+00:00","mainEntityOfPage":{"@id":"https://examples.javacodegeeks.com/java-not-equal-example/"},"wordCount":444,"commentCount":1,"publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"image":{"@id":"https://examples.javacodegeeks.com/java-not-equal-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https://examples.javacodegeeks.com/java-not-equal-example/#respond"]}]},{"@type":"WebPage","@id":"https://examples.javacodegeeks.com/java-not-equal-example/","url":"https://examples.javacodegeeks.com/java-not-equal-example/","name":"Java not equal Example - Java Code Geeks","isPartOf":{"@id":"https://examples.javacodegeeks.com/#website"},"primaryImageOfPage":{"@id":"https://examples.javacodegeeks.com/java-not-equal-example/#primaryimage"},"image":{"@id":"https://examples.javacodegeeks.com/java-not-equal-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","datePublished":"2020-01-17T09:00:00+00:00","dateModified":"2021-08-10T09:01:20+00:00","description":"Check out our detailed example about Java not equal! we will talk about what is != in Java in general and for what reason we use it.","breadcrumb":{"@id":"https://examples.javacodegeeks.com/java-not-equal-example/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://examples.javacodegeeks.com/java-not-equal-example/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/java-not-equal-example/#primaryimage","url":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https://examples.javacodegeeks.com/java-not-equal-example/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://examples.javacodegeeks.com/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https://examples.javacodegeeks.com/category/java-development/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https://examples.javacodegeeks.com/category/java-development/core-java/"},{"@type":"ListItem","position":4,"name":"Java not equal Example"}]},{"@type":"WebSite","@id":"https://examples.javacodegeeks.com/#website","url":"https://examples.javacodegeeks.com/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://examples.javacodegeeks.com/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https://examples.javacodegeeks.com/#organization","name":"Exelixis Media P.C.","url":"https://examples.javacodegeeks.com/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/logo/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https://examples.javacodegeeks.com/#/schema/logo/image/"},"sameAs":["https://www.facebook.com/javacodegeeks","https://x.com/javacodegeeks"]},{"@type":"Person","@id":"https://examples.javacodegeeks.com/#/schema/person/fa6b72698bbeff000a70a501caeb524d","name":"Ioannis Makrygiannakis","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/person/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2019/12/ioannis.makrigiannakis-96x96.png","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2019/12/ioannis.makrigiannakis-96x96.png","caption":"Ioannis Makrygiannakis"},"description":"John is an undergraduate student at the Department of Informatics of Athens University of Economics and Business. He is specialized in Databases, Knowledge Management, Information Systems and Information Security. In his free time he loves learning new things with regard to programming and network security.","url":"https://examples.javacodegeeks.com/author/ioannis-makrygiannakis/"}]}
{"@context":"http:\/\/schema.org","@type":"BreadcrumbList","@id":"#Breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"item":{"name":"Home","@id":"https:\/\/examples.javacodegeeks.com\/"}},{"@type":"ListItem","position":2,"item":{"name":"Java Development","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"}},{"@type":"ListItem","position":3,"item":{"name":"Core Java","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"}}]}
{"@context":"http:\/\/schema.org","@type":"Article","dateCreated":"2020-01-17T11:00:00+02:00","datePublished":"2020-01-17T11:00:00+02:00","dateModified":"2021-08-10T12:01:20+03:00","headline":"Java not equal Example","name":"Java not equal Example","keywords":[],"url":"https:\/\/examples.javacodegeeks.com\/java-not-equal-example\/","description":"In this article, we will show you a Java not equal Example. Firstly we will talk about what is != in Java general, and for what reason we use it. After that, we will do some examples of how we use it.","copyrightYear":"2020","articleSection":"Core Java","articleBody":"\nIn this article, we will show you a Java not equal Example. Firstly we will talk about what is != in Java general, and for what reason we use it. After that, we will do some examples of how we use it.\n\n\n\n1. Introduction\n\n\n\n Java has a rich set of operators which is used to manipulate variables. This set we can divide them into six groups :\n\n\n\n Arithmetic OperatorsRelational OperatorsBitwise OperatorsLogical OperatorsAssignment OperatorsMisc Operators \n\n\n\n The operator that we will analyze in this article is a relational operator. It is symbolized \"!=\" or \"(!a.equals(b))\" and checks if the values of two operands are equal or not, in case that values are not equal then condition becomes true. After the comparison, this operator returns a boolean value(true or false). The type of each operand that we use can be anything, for example, String, integer, double, short etc but when we compare them the type must be the same. \n\n\n\n2. Technologies used\n\n\n\nThe example code in this article was built and run using:\n\n\n\nJava 1.8.231(1.8.x will do fine)Eclipse IDE for Enterprise Java Developers- Photon\n\n\n\n3. Difference between != and !a.equals(b).\n\n\n\n The main difference between == and equals is that \"==\" is used to compare primitives while equals() method is recommended to check equality of objects. The goes for not equal.\n\n\n\n4. Java not equal Examples\n\n\n\nHere we show you some examples about != Java to understand better the use of this operator. First, we do some examples with some primitive types.\n\n\n\n Not_Equal.java \n\n\n\npublic class Not_Equal {\npublic static void main (String args[]) {\n\tbyte b1=1;\n\tbyte b2=2;\n\tint in1=3;\n\tint in2=3;\n short sh1=5;\n short sh2=6;\n long l1=7;\n long l2=8;\n float f1=9.1f;\n float f2=10.1f;\n double d1=11.01;\n double d2=11.01;\n String s1=\"Good \";\n String s2=\"Morning\";\n\t\n\tif(b1!=b2) {\n\t\tSystem.out.println(\"true\");\n\t}else {\n\t\tSystem.out.println(\"false\");\n\t}\n\t\n\tif(in1!=in2) {\n\t\tSystem.out.println(\"true\");\n\t}else {\n\t\tSystem.out.println(\"false\");\n\t}\n\t\n\tif(sh1!=sh2) {\n\t\tSystem.out.println(\"true\");\n\t}else {\n\t\tSystem.out.println(\"false\");\n\t}\n\t\n\tif(l1!=l2) {\n\t\tSystem.out.println(\"true\");\n\t}else {\n\t\tSystem.out.println(\"false\");\n\t}\n\t\n\tif(f1!=f2) {\n\t\tSystem.out.println(\"true\");\n\t}else {\n\t\tSystem.out.println(\"false\");\n\t}\n\t\n\tif(d1!=d2) {\n\t\tSystem.out.println(\"true\");\n\t}else {\n\t\tSystem.out.println(\"false\");\n\t}\n\t\n\tif(s1!=s2) {\n\t\tSystem.out.println(\"true\");\n\t}else {\n\t\tSystem.out.println(\"false\");\n\t}\n}\n}\n\n\n\n\nOutput\n\n\n\ntrue\nfalse\ntrue\ntrue\ntrue\nfalse\ntrue\n\n\n\nHere we do one example for not equal with objects.\n\n\n\nNot_Equal_Objects.java\n\n\n\npublic class Not_Equal_Objects {\n\tstatic class Car {\n\n private String name ;\n private int cc;\n private int speed;\n\n public Car(String name, int cc, int speed) {\n this.name = name;\n this.cc = cc;\n this.speed = speed;\n }\n\t}\n\tpublic static void main (String args[]) {\n\t\tCar a=new Car(\"Mercedes-Benz\", 1400, 250);\n\t\tCar b=new Car(\"Fiat\", 1200, 220);\n\t\t\n\t\t\n\t\tif(!a.equals(b)) {\n\t\t\tSystem.out.println(\"It is not the same car\");\n\t\t}\n\t\tif(a!=b) {\n\t\t\tSystem.out.println(\"It is not the same car\");\n\t\t}\n\t}\n}\n\n\n\n\nOutput\n\n\n\n It is not the same car\nIt is not the same car \n\n\n\n\nAs you can see in lines 19 and 22 we can use both versions.\n\n\n\nHere you can see an example with an override method. Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. This new method has the same name, same parameters or signature and same return type as a method in its super-class.\n\n\n\nNot_Equal_Override.java\n\n\n\n\tpublic class Not_equal_override {\n\t\tstatic class Car {\n\n\t private static String name ;\n\t private int cc;\n\t private int speed;\n\n\t public Car(String name, int cc, int speed) {\n\t this.name = name;\n\t this.cc = cc;\n\t this.speed = speed;\n\t }\n\t\t\n\t\t@Override\n\t\tpublic boolean equals (Object o) {\n\t\tCar car= (Car) o;\n\t\tif(this.name.equals(car.name)) {\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t\t}\t\n\t\t}\n\t\t\n\t\tpublic static void main (String args[]) {\n\t\t\t\n\t\t\tCar b=new Car(\"Fiat\", 1200, 220);\n\t\t\tCar c=new Car(\"Fiat\", 1234, 144);\n\t\t\t\n\t\t\tif(!b.equals(c)) {\n\t\t\t\tSystem.out.println(\"The cars have not the same name\");\n\t\t\t}else {\n\t\t\t\tSystem.out.println(\"The cars have the same name\");\n\t\t\t}\n\t\t}\n\t}\n\n\n\n\nOutput\n\n\n\n The cars have the same name \n\n\n\n\nIn the above code, you can see how to do an override not equal method so that we can compare only the name of the cars.\n\n\n\n5. More articles\n\n\n\nJava Tutorial for Beginners (with video)For loop Java Example (with video)Java String Class Example (with video)Math.random Java ExampleJava random number generator ExampleString CompareTo Java ExamplePrintf Java Example (with video)\n\n\n\n6. Download the Complete Source Code\n\n\n\nDownload\nYou can download the full source code of this example here: Java not equal Example\n\n\n\nLast updated on Aug. 10th, 2021\n","publisher":{"@id":"#Publisher","@type":"Organization","name":"Examples Java Code Geeks","logo":{"@type":"ImageObject","url":"\/wp-content\/uploads\/2013\/05\/JavaCodeGeeks-logo.png"},"sameAs":["https:\/\/feeds.feedburner.com\/examplesjavacodegeeks","https:\/\/www.facebook.com\/javacodegeeks","https:\/\/twitter.com\/javacodegeeks","https:\/\/www.linkedin.com\/groups\/3810709\/","https:\/\/www.youtube.com\/channel\/UCxoUc7Rar2q90Gu0nT2ffuQ","https:\/\/github.com\/javacodegeeks\/"]},"sourceOrganization":{"@id":"#Publisher"},"copyrightHolder":{"@id":"#Publisher"},"mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-not-equal-example\/","breadcrumb":{"@id":"#Breadcrumb"}},"author":{"@type":"Person","name":"Ioannis Makrygiannakis","url":"https:\/\/examples.javacodegeeks.com\/author\/ioannis-makrygiannakis\/"},"image":{"@type":"ImageObject","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":1200,"height":150}}
| og:locale | en_US |
| og:type | article |
| og:site_name | Examples Java Code Geeks |
| article:publisher | https://www.facebook.com/javacodegeeks |
| article:published_time | 2020-01-17T09:00:00+00:00 |
| article:modified_time | 2021-08-10T09:01:20+00:00 |
| og:image | https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg |
| og:image:width | 150 |
| og:image:height | 150 |
| og:image:type | image/jpeg |
| author | Ioannis Makrygiannakis |
| twitter:card | summary_large_image |
| twitter:creator | @javacodegeeks |
| twitter:label1 | Written by |
| twitter:data1 | Ioannis Makrygiannakis |
| twitter:label2 | Est. reading time |
| twitter:data2 | 4 minutes |
| None | IE=edge |
| msapplication-TileImage | https://examples.javacodegeeks.com/wp-content/uploads/2013/05/JavaCodeGeeks-favIcon.png |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1