Found 34486 Articles for Programming

Swap two variables in one line in using Java

Samual Sam
Updated on 30-Jul-2019 22:30:23

5K+ Views

Two variables can be swapped in one line in Java. This is done by using the given statement. x = x ^ y ^ (y = x); where x and y are the 2 variables. A program that demonstrates this is given as follows − Example Live Demo public class Example { public static void main (String[] args) { int x = 12, y = 25; System.out.println("Original values of x and y"); System.out.println("x = " ... Read More

Java program to count total bits in a number

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

673 Views

The total bits in a number can be counted by using its binary representation. An example of this is given as follows − Number = 9 Binary representation = 1001 Total bits = 4 A program that demonstrates this is given as follows. Example Live Demo public class Example { public static void main(String[] arg) { int num = 10; int n = num; int count = 0; while ... Read More

How to access unique Android device ID?

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

2K+ Views

If you want to check unique device id like IMEI number through programmatically we can do this by telephonic manger as shown below example −Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     Step 3 − Add the following code to src/MainActivity.javaimport android.Manifest; import android.annotation.SuppressLint; import android.app.ProgressDialog; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.support.annotation.RequiresApi; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.telephony.TelephonyManager; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import ... Read More

How to create a notification with NotificationCompat.Builder in Android?

George John
Updated on 26-Jun-2020 05:38:04

3K+ Views

Before getting into NotificationCompact.Builder, we should know what is a notification in android. Notification is just like as a message showing system on the action bar. just like missed call notification as shown belowThis example demonstrates how to integrate Android Notification.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project, and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     Step 3 − Add the following code to src/MainActivity.javapackage com.example.andy.myapplication; import android.annotation.SuppressLint; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import ... Read More

Java program to display Astrological sign or Zodiac sign for given date of birth

karthikeya Boyini
Updated on 26-Jun-2020 05:42:05

6K+ Views

Each date of birth corresponds to a given Zodiac sign. A table that demonstrates these signs and their corresponding dates is given below −Zodiac SignDateAriesMarch 21 - April 19TaurusApril 20 - May 20GeminiMay 21 - June 20CancerJune 21 - July 22LeoJuly 23 - August 22VirgoAugust 23 - September 22LibraSeptember 23 - October 22ScorpioOctober 23 - November 21SagittariusNovember 22 - December 21CapricornDecember 22 - January 19AquariusJanuary 20 - February 18PiscesFebruary 19 - March 20A program that displays the Astrological sign or Zodiac sign for a given date of birth is given as follows.Example Live Demopublic class Example {    public static void ... Read More

Define integer literals as octal values in Java

Samual Sam
Updated on 30-Jul-2019 22:30:23

3K+ Views

Literals with a leading zero are octal literals. Any number prefixed with a 0 is considered octal. Octal numbers can only use digits 0-7, just like decimal can use 0-9, and binary can use 0-1. To define integer literals as octal value in Java is effortless. Here is the declaration and initialization. int myOct = 023; Example Live Demo public class Demo { public static void main(String []args) { int myOct = 023; System.out.println(myOct); } } Output 19 Let us see another example. Example Live Demo public class Demo { public static void main(String []args) { int myOct = 010; System.out.println(myOct); } } Output 8

How do I display an alert dialog on Android?

Chandu yadav
Updated on 30-Jul-2019 22:30:23

3K+ Views

Before getting into alert dialog, we should know about what is alert dialog, Alert dialog is just like a pop-up where user can choose action by clicking "ok" or "cancel" button.Methods in Alert DialogsetView(View view) − It used to set custom view to alert dialogsetTitle(CharSequence title) − It is used to set title to alert dialogsetMessage(CharSequence message) − It is simple call as content in alert boxsetIcon(int resId) − it is used to set icon for alert boxsetButton(int whichButton,  CharSequence text,  Message msg) − It is used to set button for alert dialog as shown below example.getListView() − it is used to ... Read More

Hexadecimal literal of type long in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

892 Views

Hexadecimal literal of type long is represented as − long hexLong = 0XABL; For Hexadecimal, the 0x or 0X is to be placed in the beginning of a number. Note − Digits 10 to 15 are represented by a to f (A to F) in Hexadecimal Example Live Demo public class Demo { public static void main(String []args) { long hexLong = 0XABL; System.out.println("Hexadecimal literal of type long: "+hexLong); } } Output Hexadecimal literal of type long:171

Handler in android ?

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

12K+ Views

We cant touch background thread to main thread directly so handler is going to collect all events which are available in main thread in a queue and posses this queue to looper class.In android Handler is mainly used to update the main thread from background thread or other than main thread. There are two methods are in handler.Post() − it going to post message from background thread to main thread using looper.sendmessage() − if you want to organize what you have sent to ui (message from background thread) or ui functions. you should use sendMessage().This example demonstrate about how to ... Read More

Hexadecimal integer literal in Java

Samual Sam
Updated on 30-Jul-2019 22:30:23

5K+ Views

For Hexadecimal, the 0x or 0X is to be placed in the beginning of a number. Note − Digits 10 to 15 are represented by a to f (A to F) in Hexadecimal Here are some of the examples of hexadecimal integer literal declared and initialized as int. int one = 0X123; int two = 0xABC; Example Live Demo public class Demo { public static void main(String []args) { int one = 0X123; int two = 0xABC; System.out.println("Hexadecimal: "+one); System.out.println("Hexadecimal: "+two); } } Output Hexadecimal: 291 Hexadecimal: 2748

Advertisements