Found 31499 Articles for Programming

Can you have a method in Java with varying number of arguments?

Prabhas
Updated on 16-Jun-2020 09:34:09
Yes, we can write a method using variable arguments once you use variable arguments as a parameter method while calling you can pass as many numbers of arguments to this method (variable number of arguments) or, you can simply call this method without passing any arguments.ExampleLive Demopublic class Sample{    void demoMethod(String... args) {       for (String arg : args) {          System.out.println(arg);       }    }    public static void main(String args[] ){       new Sample().demoMethod("ram", "rahim", "robert");       new Sample().demoMethod("krishna", "kasyap");       new Sample().demoMethod();    } }Outputram rahim robert krishna kasyap

How to write binary data to a file using Python?

Rajendra Dharmkar
Updated on 13-Dec-2019 10:31:33
"Binary" files are any files where the format isn't made up of readable characters. Binary files can range from image files like JPEGs or GIFs, audio files like MP3s or binary document formats like Word or PDF. In Python, files are opened in text mode by default. To open files in binary mode, when specifying a mode, add 'b' to it.For examplef = open('my_file', 'w+b') byte_arr = [120, 3, 255, 0, 100] binary_format = bytearray(byte_arr) f.write(binary_format) f.close()This opens a file in binary write mode and writes the byte_arr array contents as bytes in the binary file, my_file.Read More

What is method overloading in Java?

seetha
Updated on 30-Jul-2019 22:30:20
When a class has two or more methods by the same name but different parameters, at the time of calling based on the parameters passed respective method is called (or respective method body will be bonded with the calling line dynamically). This mechanism is known as method overloading. If you observe the following example, Here we have created a class named Sample and this class has two methods with same name (add) and return type, the only difference is the parameters they accept (one method accepts two integer variables and other accepts three integer variables). When you invoke the add() ... Read More

Are there inline functions in Java?

vanithasree
Updated on 30-Jul-2019 22:30:20
If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time. Any change to an inline function could require all clients of the function to be recompiled because compiler would need to replace all the code once again otherwise it will continue with old functionality. No, Java does not provide inline functions it is typically done by the JVM at execution time.

What is the difference between non-static methods and abstract methods in Java?

radhakrishna
Updated on 19-Dec-2019 06:36:11
Following are the notable differences between non-static methods and abstract methods.Non-static (normal) methodsAbstract methodsThese methods contain a body.Abstract methods don’t have body these are ended with a semicolonYou can use normal method directly.You cannot use abstract methods directly, to use them you need to inherit them and provide body to these methods and use them.Example:public void display() {    System.out.println("Hi"); }Example:public void display();

How to set creation and modification date/time of a file using Python?

Rajendra Dharmkar
Updated on 13-Dec-2019 10:34:18
To get the creation time of a file, you can use the os.path.getctime(file_path) on windows. On UNIX systems, you cant use the same function as it returns the last time that the file's attributes or content were changed. To get the creation time on UNIX based systems, use the st_birthtime attribute of the stat tuple.ExampleOn Windows:>>> import os >>> print os.path.getctime('my_file') 1505928271.0689342It gives the time in the number of seconds since the epoch. For UNIX systems, import os stat = os.stat(path_to_file) try:     print(stat.st_birthtime) except AttributeError:     # Probably on Linux. No easy way to get creation dates ... Read More

How to get creation and modification date/time of a file using Python?

Rajendra Dharmkar
Updated on 18-Feb-2020 04:59:41
To get the creation time of a file, you can use the os.path.getctime(file_path) on windows. On UNIX systems, you cant use the same function as it returns the last time that the file's attributes or content were changed. To get the creation time on UNIX based systems, use the st_birthtime attribute of the stat tuple. exampleOn Windows −>>> import os >>> print os.path.getctime('my_file') 1505928271.0689342It gives the time in the number of seconds since the epoch. For UNIX systems, import os stat = os.stat(path_to_file) try:     print(stat.st_birthtime) except AttributeError:     # Probably on Linux. No easy way to get creation ... Read More

How to replace Digits into String using Java?

Malhar Lathkar
Updated on 20-Jun-2020 13:19:39
For this purpose, we create an object of HashMap class which is defined in java.util packageMap map = new HashMap();This hashmap object associates each digit with its corresponding word representationmap.put("0", "zero");Initialize an empty string object.String newstr="";Next, run a for loop over the length of given string and extract each character by substring() method of String class.Check if the character exists as a key in map object by containsKey() method. If it does, using it as key, obtain its value component in map and appenf\d to the new string. If not append the character itself to the new string. The complete code ... Read More

How do I write variable names in Java?

Sreemaha
Updated on 30-Jul-2019 22:30:20
While choosing an identifier to declare a variable in Java you need to keep the following points in mind. The name of the variable should begin with either alphabet or, an underscore (_) or, a dollar ($) sign. The identifiers used for variables must not be keywords. No spaces or special characters are allowed in the variable names of Java. Variable names may contain 0 to 9 numbers (if not at the beginning). Variable names are case sensitive i.e. MY_NUM is different from my_num. If you use two words in a identifier then the you should follow camel case ... Read More

How to remove a file using Python?

Rajendra Dharmkar
Updated on 18-Feb-2020 04:57:20
You can delete a single file or a single empty folder with functions in the os module. For example, if you want to delete a file a.txt,>>> import os >>> os.remove('a.txt')The argument to os.remove must be absolute or relative path. You can also use the os.unlink() remove files. For example,>>> import os >>> os.unlink('a.txt')
Advertisements